]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - scripts/checkpatch.pl
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[mirror_ubuntu-jammy-kernel.git] / scripts / checkpatch.pl
CommitLineData
cb77f0d6 1#!/usr/bin/env perl
882ea1d6
JP
2# SPDX-License-Identifier: GPL-2.0
3#
dbf004d7 4# (c) 2001, Dave Jones. (the file handling bit)
00df344f 5# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
2a5a2c25 6# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
015830be 7# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
882ea1d6 8# (c) 2010-2018 Joe Perches <joe@perches.com>
0a920b5b
AW
9
10use strict;
cb77f0d6 11use warnings;
c707a81d 12use POSIX;
36061e38
JP
13use File::Basename;
14use Cwd 'abs_path';
57230297 15use Term::ANSIColor qw(:constants);
cd261496 16use Encode qw(decode encode);
0a920b5b
AW
17
18my $P = $0;
36061e38 19my $D = dirname(abs_path($P));
0a920b5b 20
000d1cc1 21my $V = '0.32';
0a920b5b
AW
22
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
773647a0 29my $tst_only;
6c72ffaa 30my $emacs = 0;
8905a67c 31my $terse = 0;
34d8815f 32my $showfile = 0;
6c72ffaa 33my $file = 0;
4a593c34 34my $git = 0;
0dea9f1e 35my %git_commits = ();
6c72ffaa 36my $check = 0;
2ac73b4f 37my $check_orig = 0;
8905a67c
AW
38my $summary = 1;
39my $mailback = 0;
13214adf 40my $summary_file = 0;
000d1cc1 41my $show_types = 0;
3beb42ec 42my $list_types = 0;
3705ce5b 43my $fix = 0;
9624b8d6 44my $fix_inplace = 0;
6c72ffaa 45my $root;
0f7f635b
JP
46my $gitroot = $ENV{'GIT_DIR'};
47$gitroot = ".git" if !defined($gitroot);
c2fdda0d 48my %debug;
3445686a 49my %camelcase = ();
91bfe484
JP
50my %use_type = ();
51my @use = ();
52my %ignore_type = ();
000d1cc1 53my @ignore = ();
77f5b10a 54my $help = 0;
000d1cc1 55my $configuration_file = ".checkpatch.conf";
bdc48fa1 56my $max_line_length = 100;
d62a201f
DH
57my $ignore_perl_version = 0;
58my $minimum_perl_version = 5.10.0;
56193274 59my $min_conf_desc_length = 4;
66b47b4a 60my $spelling_file = "$D/spelling.txt";
ebfd7d62 61my $codespell = 0;
f1a63678 62my $codespellfile = "/usr/share/codespell/dictionary.txt";
bf1fa1da 63my $conststructsfile = "$D/const_structs.checkpatch";
ced69da1 64my $typedefsfile;
737c0767 65my $color = "auto";
98005e8c 66my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
dbbf869d
JP
67# git output parsing needs US English output, so first set backtick child process LANGUAGE
68my $git_command ='export LANGUAGE=en_US.UTF-8; git';
713a09de 69my $tabsize = 8;
3e89ad85 70my ${CONFIG_} = "CONFIG_";
77f5b10a
HE
71
72sub help {
73 my ($exitcode) = @_;
74
75 print << "EOM";
76Usage: $P [OPTION]... [FILE]...
77Version: $V
78
79Options:
80 -q, --quiet quiet
81 --no-tree run without a kernel tree
82 --no-signoff do not check for 'Signed-off-by' line
83 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
34d8815f 86 --showfile emit diffed file position, not input file position
4a593c34
CD
87 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
77f5b10a
HE
97 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
3beb42ec 99 --list-types list the possible message types
91bfe484 100 --types TYPE(,TYPE2...) show only these comma separated message types
000d1cc1 101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
3beb42ec 102 --show-types show the specific message type in the output
bdc48fa1
JP
103 --max-line-length=n set the maximum line length, (default $max_line_length)
104 if exceeded, warn on patches
105 requires --strict for use with --file
56193274 106 --min-conf-desc-length=n set the min description length, if shorter, warn
bdc48fa1 107 --tab-size=n set the number of spaces for tab (default $tabsize)
77f5b10a
HE
108 --root=PATH PATH to the kernel tree root
109 --no-summary suppress the per-file summary
110 --mailback only produce a report in case of warnings/errors
111 --summary-file include the filename in summary
112 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
113 'values', 'possible', 'type', and 'attr' (default
114 is all off)
115 --test-only=WORD report only warnings/errors containing WORD
116 literally
3705ce5b
JP
117 --fix EXPERIMENTAL - may create horrible results
118 If correctable single-line errors exist, create
119 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
120 with potential errors corrected to the preferred
121 checkpatch style
9624b8d6
JP
122 --fix-inplace EXPERIMENTAL - may create horrible results
123 Is the same as --fix, but overwrites the input
124 file. It's your fault if there's no backup or git
d62a201f
DH
125 --ignore-perl-version override checking of perl version. expect
126 runtime errors.
ebfd7d62 127 --codespell Use the codespell dictionary for spelling/typos
f1a63678 128 (default:/usr/share/codespell/dictionary.txt)
ebfd7d62 129 --codespellfile Use this codespell dictionary
75ad8c57 130 --typedefsfile Read additional types from this file
737c0767
JB
131 --color[=WHEN] Use colors 'always', 'never', or only when output
132 is a terminal ('auto'). Default is 'auto'.
3e89ad85
JF
133 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
134 ${CONFIG_})
77f5b10a
HE
135 -h, --help, --version display this help and exit
136
137When FILE is - read standard input.
138EOM
139
140 exit($exitcode);
141}
142
3beb42ec
JP
143sub uniq {
144 my %seen;
145 return grep { !$seen{$_}++ } @_;
146}
147
148sub list_types {
149 my ($exitcode) = @_;
150
151 my $count = 0;
152
153 local $/ = undef;
154
155 open(my $script, '<', abs_path($P)) or
156 die "$P: Can't read '$P' $!\n";
157
158 my $text = <$script>;
159 close($script);
160
161 my @types = ();
0547fa58
JD
162 # Also catch when type or level is passed through a variable
163 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
3beb42ec
JP
164 push (@types, $_);
165 }
166 @types = sort(uniq(@types));
167 print("#\tMessage type\n\n");
168 foreach my $type (@types) {
169 print(++$count . "\t" . $type . "\n");
170 }
171
172 exit($exitcode);
173}
174
000d1cc1
JP
175my $conf = which_conf($configuration_file);
176if (-f $conf) {
177 my @conf_args;
178 open(my $conffile, '<', "$conf")
179 or warn "$P: Can't find a readable $configuration_file file $!\n";
180
181 while (<$conffile>) {
182 my $line = $_;
183
184 $line =~ s/\s*\n?$//g;
185 $line =~ s/^\s*//g;
186 $line =~ s/\s+/ /g;
187
188 next if ($line =~ m/^\s*#/);
189 next if ($line =~ m/^\s*$/);
190
191 my @words = split(" ", $line);
192 foreach my $word (@words) {
193 last if ($word =~ m/^#/);
194 push (@conf_args, $word);
195 }
196 }
197 close($conffile);
198 unshift(@ARGV, @conf_args) if @conf_args;
199}
200
737c0767
JB
201# Perl's Getopt::Long allows options to take optional arguments after a space.
202# Prevent --color by itself from consuming other arguments
203foreach (@ARGV) {
204 if ($_ eq "--color" || $_ eq "-color") {
205 $_ = "--color=$color";
206 }
207}
208
0a920b5b 209GetOptions(
6c72ffaa 210 'q|quiet+' => \$quiet,
0a920b5b
AW
211 'tree!' => \$tree,
212 'signoff!' => \$chk_signoff,
213 'patch!' => \$chk_patch,
6c72ffaa 214 'emacs!' => \$emacs,
8905a67c 215 'terse!' => \$terse,
34d8815f 216 'showfile!' => \$showfile,
77f5b10a 217 'f|file!' => \$file,
4a593c34 218 'g|git!' => \$git,
6c72ffaa
AW
219 'subjective!' => \$check,
220 'strict!' => \$check,
000d1cc1 221 'ignore=s' => \@ignore,
91bfe484 222 'types=s' => \@use,
000d1cc1 223 'show-types!' => \$show_types,
3beb42ec 224 'list-types!' => \$list_types,
6cd7f386 225 'max-line-length=i' => \$max_line_length,
56193274 226 'min-conf-desc-length=i' => \$min_conf_desc_length,
713a09de 227 'tab-size=i' => \$tabsize,
6c72ffaa 228 'root=s' => \$root,
8905a67c
AW
229 'summary!' => \$summary,
230 'mailback!' => \$mailback,
13214adf 231 'summary-file!' => \$summary_file,
3705ce5b 232 'fix!' => \$fix,
9624b8d6 233 'fix-inplace!' => \$fix_inplace,
d62a201f 234 'ignore-perl-version!' => \$ignore_perl_version,
c2fdda0d 235 'debug=s' => \%debug,
773647a0 236 'test-only=s' => \$tst_only,
ebfd7d62
JP
237 'codespell!' => \$codespell,
238 'codespellfile=s' => \$codespellfile,
75ad8c57 239 'typedefsfile=s' => \$typedefsfile,
737c0767
JB
240 'color=s' => \$color,
241 'no-color' => \$color, #keep old behaviors of -nocolor
242 'nocolor' => \$color, #keep old behaviors of -nocolor
3e89ad85 243 'kconfig-prefix=s' => \${CONFIG_},
77f5b10a
HE
244 'h|help' => \$help,
245 'version' => \$help
246) or help(1);
247
248help(0) if ($help);
0a920b5b 249
3beb42ec
JP
250list_types(0) if ($list_types);
251
9624b8d6 252$fix = 1 if ($fix_inplace);
2ac73b4f 253$check_orig = $check;
9624b8d6 254
32f30ca9
JP
255die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
256
0a920b5b
AW
257my $exit = 0;
258
5b57980d 259my $perl_version_ok = 1;
d62a201f 260if ($^V && $^V lt $minimum_perl_version) {
5b57980d 261 $perl_version_ok = 0;
d62a201f 262 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
5b57980d 263 exit(1) if (!$ignore_perl_version);
d62a201f
DH
264}
265
45107ff6 266#if no filenames are given, push '-' to read patch from stdin
0a920b5b 267if ($#ARGV < 0) {
45107ff6 268 push(@ARGV, '-');
0a920b5b
AW
269}
270
737c0767
JB
271if ($color =~ /^[01]$/) {
272 $color = !$color;
273} elsif ($color =~ /^always$/i) {
274 $color = 1;
275} elsif ($color =~ /^never$/i) {
276 $color = 0;
277} elsif ($color =~ /^auto$/i) {
278 $color = (-t STDOUT);
279} else {
32f30ca9 280 die "$P: Invalid color mode: $color\n";
737c0767
JB
281}
282
713a09de 283# skip TAB size 1 to avoid additional checks on $tabsize - 1
32f30ca9 284die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
713a09de 285
91bfe484
JP
286sub hash_save_array_words {
287 my ($hashRef, $arrayRef) = @_;
288
289 my @array = split(/,/, join(',', @$arrayRef));
290 foreach my $word (@array) {
291 $word =~ s/\s*\n?$//g;
292 $word =~ s/^\s*//g;
293 $word =~ s/\s+/ /g;
294 $word =~ tr/[a-z]/[A-Z]/;
295
296 next if ($word =~ m/^\s*#/);
297 next if ($word =~ m/^\s*$/);
298
299 $hashRef->{$word}++;
300 }
301}
000d1cc1 302
91bfe484
JP
303sub hash_show_words {
304 my ($hashRef, $prefix) = @_;
000d1cc1 305
3c816e49 306 if (keys %$hashRef) {
d8469f16 307 print "\nNOTE: $prefix message types:";
58cb3cf6 308 foreach my $word (sort keys %$hashRef) {
91bfe484
JP
309 print " $word";
310 }
d8469f16 311 print "\n";
91bfe484 312 }
000d1cc1
JP
313}
314
91bfe484
JP
315hash_save_array_words(\%ignore_type, \@ignore);
316hash_save_array_words(\%use_type, \@use);
317
c2fdda0d
AW
318my $dbg_values = 0;
319my $dbg_possible = 0;
7429c690 320my $dbg_type = 0;
a1ef277e 321my $dbg_attr = 0;
c2fdda0d 322for my $key (keys %debug) {
21caa13c
AW
323 ## no critic
324 eval "\${dbg_$key} = '$debug{$key}';";
325 die "$@" if ($@);
c2fdda0d
AW
326}
327
d2c0a235
AW
328my $rpt_cleaners = 0;
329
8905a67c
AW
330if ($terse) {
331 $emacs = 1;
332 $quiet++;
333}
334
6c72ffaa
AW
335if ($tree) {
336 if (defined $root) {
337 if (!top_of_kernel_tree($root)) {
338 die "$P: $root: --root does not point at a valid tree\n";
339 }
340 } else {
341 if (top_of_kernel_tree('.')) {
342 $root = '.';
343 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
344 top_of_kernel_tree($1)) {
345 $root = $1;
346 }
347 }
348
349 if (!defined $root) {
350 print "Must be run from the top-level dir. of a kernel tree\n";
351 exit(2);
352 }
0a920b5b
AW
353}
354
6c72ffaa
AW
355my $emitted_corrupt = 0;
356
2ceb532b
AW
357our $Ident = qr{
358 [A-Za-z_][A-Za-z\d_]*
359 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
360 }x;
6c72ffaa
AW
361our $Storage = qr{extern|static|asmlinkage};
362our $Sparse = qr{
363 __user|
364 __kernel|
365 __force|
366 __iomem|
367 __must_check|
417495ed 368 __kprobes|
165e72a6 369 __ref|
33aa4597
GU
370 __refconst|
371 __refdata|
ad315455
BF
372 __rcu|
373 __private
6c72ffaa 374 }x;
e970b884
JP
375our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
8716de38 380
52131292
WS
381# Notes to $Attribute:
382# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
6c72ffaa
AW
383our $Attribute = qr{
384 const|
03f1df7d
JP
385 __percpu|
386 __nocast|
387 __safe|
46d832f5 388 __bitwise|
03f1df7d
JP
389 __packed__|
390 __packed2__|
391 __naked|
392 __maybe_unused|
393 __always_unused|
394 __noreturn|
395 __used|
396 __cold|
e23ef1f3 397 __pure|
03f1df7d
JP
398 __noclone|
399 __deprecated|
6c72ffaa 400 __read_mostly|
c5967e98 401 __ro_after_init|
6c72ffaa 402 __kprobes|
8716de38 403 $InitAttribute|
24e1d81a
AW
404 ____cacheline_aligned|
405 ____cacheline_aligned_in_smp|
5fe3af11
AW
406 ____cacheline_internodealigned_in_smp|
407 __weak
6c72ffaa 408 }x;
c45dcabd 409our $Modifier;
91cb5195 410our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
6c72ffaa
AW
411our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
412our $Lval = qr{$Ident(?:$Member)*};
413
95e2c602
JP
414our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
415our $Binary = qr{(?i)0b[01]+$Int_type?};
416our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
417our $Int = qr{[0-9]+$Int_type?};
2435880f 418our $Octal = qr{0[0-7]+$Int_type?};
c0a5c898 419our $String = qr{"[X\t]*"};
326b1ffc
JP
420our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
421our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
422our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
74349bcc 423our $Float = qr{$Float_hex|$Float_dec|$Float_int};
2435880f 424our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
326b1ffc 425our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
447432f3 426our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
23f780c9 427our $Arithmetic = qr{\+|-|\*|\/|%};
6c72ffaa
AW
428our $Operators = qr{
429 <=|>=|==|!=|
430 =>|->|<<|>>|<|>|!|~|
23f780c9 431 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
6c72ffaa
AW
432 }x;
433
91cb5195
JP
434our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
435
ab7e23f3 436our $BasicType;
8905a67c 437our $NonptrType;
1813087d 438our $NonptrTypeMisordered;
8716de38 439our $NonptrTypeWithAttr;
8905a67c 440our $Type;
1813087d 441our $TypeMisordered;
8905a67c 442our $Declare;
1813087d 443our $DeclareMisordered;
8905a67c 444
15662b3e
JP
445our $NON_ASCII_UTF8 = qr{
446 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
171ae1a4
AW
447 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
448 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
449 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
450 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
451 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
452 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
453}x;
454
15662b3e
JP
455our $UTF8 = qr{
456 [\x09\x0A\x0D\x20-\x7E] # ASCII
457 | $NON_ASCII_UTF8
458}x;
459
e6176fa4 460our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
021158b4
JP
461our $typeOtherOSTypedefs = qr{(?x:
462 u_(?:char|short|int|long) | # bsd
463 u(?:nchar|short|int|long) # sysv
464)};
e6176fa4 465our $typeKernelTypedefs = qr{(?x:
fb9e9096 466 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
8ed22cad
AW
467 atomic_t
468)};
e6176fa4
JP
469our $typeTypedefs = qr{(?x:
470 $typeC99Typedefs\b|
471 $typeOtherOSTypedefs\b|
472 $typeKernelTypedefs\b
473)};
8ed22cad 474
6d32f7a3
JP
475our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
476
691e669b 477our $logFunctions = qr{(?x:
758d7aad 478 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
7d0b6594 479 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
87bd499a 480 TP_printk|
6e60c02e 481 WARN(?:_RATELIMIT|_ONCE|)|
b0531722 482 panic|
06668727
JP
483 MODULE_[A-Z_]+|
484 seq_vprintf|seq_printf|seq_puts
691e669b
JP
485)};
486
e29a70f1
JP
487our $allocFunctions = qr{(?x:
488 (?:(?:devm_)?
489 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
490 kstrdup(?:_const)? |
491 kmemdup(?:_nul)?) |
461e1565 492 (?:\w+)?alloc_skb(?:_ip_align)? |
e29a70f1
JP
493 # dev_alloc_skb/netdev_alloc_skb, et al
494 dma_alloc_coherent
495)};
496
20112475
JP
497our $signature_tags = qr{(?xi:
498 Signed-off-by:|
d499480c 499 Co-developed-by:|
20112475
JP
500 Acked-by:|
501 Tested-by:|
502 Reviewed-by:|
503 Reported-by:|
8543ae12 504 Suggested-by:|
20112475
JP
505 To:|
506 Cc:
507)};
508
831242ab
AS
509sub edit_distance_min {
510 my (@arr) = @_;
511 my $len = scalar @arr;
512 if ((scalar @arr) < 1) {
513 # if underflow, return
514 return;
515 }
516 my $min = $arr[0];
517 for my $i (0 .. ($len-1)) {
518 if ($arr[$i] < $min) {
519 $min = $arr[$i];
520 }
521 }
522 return $min;
523}
524
525sub get_edit_distance {
526 my ($str1, $str2) = @_;
527 $str1 = lc($str1);
528 $str2 = lc($str2);
529 $str1 =~ s/-//g;
530 $str2 =~ s/-//g;
531 my $len1 = length($str1);
532 my $len2 = length($str2);
533 # two dimensional array storing minimum edit distance
534 my @distance;
535 for my $i (0 .. $len1) {
536 for my $j (0 .. $len2) {
537 if ($i == 0) {
538 $distance[$i][$j] = $j;
539 } elsif ($j == 0) {
540 $distance[$i][$j] = $i;
541 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
542 $distance[$i][$j] = $distance[$i - 1][$j - 1];
543 } else {
544 my $dist1 = $distance[$i][$j - 1]; #insert distance
545 my $dist2 = $distance[$i - 1][$j]; # remove
546 my $dist3 = $distance[$i - 1][$j - 1]; #replace
547 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
548 }
549 }
550 }
551 return $distance[$len1][$len2];
552}
553
554sub find_standard_signature {
555 my ($sign_off) = @_;
556 my @standard_signature_tags = (
557 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
558 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
559 );
560 foreach my $signature (@standard_signature_tags) {
561 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
562 }
563
564 return "";
565}
566
1813087d
JP
567our @typeListMisordered = (
568 qr{char\s+(?:un)?signed},
569 qr{int\s+(?:(?:un)?signed\s+)?short\s},
570 qr{int\s+short(?:\s+(?:un)?signed)},
571 qr{short\s+int(?:\s+(?:un)?signed)},
572 qr{(?:un)?signed\s+int\s+short},
573 qr{short\s+(?:un)?signed},
574 qr{long\s+int\s+(?:un)?signed},
575 qr{int\s+long\s+(?:un)?signed},
576 qr{long\s+(?:un)?signed\s+int},
577 qr{int\s+(?:un)?signed\s+long},
578 qr{int\s+(?:un)?signed},
579 qr{int\s+long\s+long\s+(?:un)?signed},
580 qr{long\s+long\s+int\s+(?:un)?signed},
581 qr{long\s+long\s+(?:un)?signed\s+int},
582 qr{long\s+long\s+(?:un)?signed},
583 qr{long\s+(?:un)?signed},
584);
585
8905a67c
AW
586our @typeList = (
587 qr{void},
0c773d9d
JP
588 qr{(?:(?:un)?signed\s+)?char},
589 qr{(?:(?:un)?signed\s+)?short\s+int},
590 qr{(?:(?:un)?signed\s+)?short},
591 qr{(?:(?:un)?signed\s+)?int},
592 qr{(?:(?:un)?signed\s+)?long\s+int},
593 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
594 qr{(?:(?:un)?signed\s+)?long\s+long},
595 qr{(?:(?:un)?signed\s+)?long},
596 qr{(?:un)?signed},
8905a67c
AW
597 qr{float},
598 qr{double},
599 qr{bool},
8905a67c
AW
600 qr{struct\s+$Ident},
601 qr{union\s+$Ident},
602 qr{enum\s+$Ident},
603 qr{${Ident}_t},
604 qr{${Ident}_handler},
605 qr{${Ident}_handler_fn},
1813087d 606 @typeListMisordered,
8905a67c 607);
938224b5
JP
608
609our $C90_int_types = qr{(?x:
610 long\s+long\s+int\s+(?:un)?signed|
611 long\s+long\s+(?:un)?signed\s+int|
612 long\s+long\s+(?:un)?signed|
613 (?:(?:un)?signed\s+)?long\s+long\s+int|
614 (?:(?:un)?signed\s+)?long\s+long|
615 int\s+long\s+long\s+(?:un)?signed|
616 int\s+(?:(?:un)?signed\s+)?long\s+long|
617
618 long\s+int\s+(?:un)?signed|
619 long\s+(?:un)?signed\s+int|
620 long\s+(?:un)?signed|
621 (?:(?:un)?signed\s+)?long\s+int|
622 (?:(?:un)?signed\s+)?long|
623 int\s+long\s+(?:un)?signed|
624 int\s+(?:(?:un)?signed\s+)?long|
625
626 int\s+(?:un)?signed|
627 (?:(?:un)?signed\s+)?int
628)};
629
485ff23e 630our @typeListFile = ();
8716de38
JP
631our @typeListWithAttr = (
632 @typeList,
633 qr{struct\s+$InitAttribute\s+$Ident},
634 qr{union\s+$InitAttribute\s+$Ident},
635);
636
c45dcabd
AW
637our @modifierList = (
638 qr{fastcall},
639);
485ff23e 640our @modifierListFile = ();
8905a67c 641
2435880f
JP
642our @mode_permission_funcs = (
643 ["module_param", 3],
644 ["module_param_(?:array|named|string)", 4],
645 ["module_param_array_named", 5],
646 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
647 ["proc_create(?:_data|)", 2],
459cf0ae
JP
648 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
649 ["IIO_DEV_ATTR_[A-Z_]+", 1],
650 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
651 ["SENSOR_TEMPLATE(?:_2|)", 3],
652 ["__ATTR", 2],
2435880f
JP
653);
654
1a3dcf2e
JP
655my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
656
515a235e
JP
657#Create a search pattern for all these functions to speed up a loop below
658our $mode_perms_search = "";
659foreach my $entry (@mode_permission_funcs) {
660 $mode_perms_search .= '|' if ($mode_perms_search ne "");
661 $mode_perms_search .= $entry->[0];
662}
00180468 663$mode_perms_search = "(?:${mode_perms_search})";
515a235e 664
9189c7e7
JP
665our %deprecated_apis = (
666 "synchronize_rcu_bh" => "synchronize_rcu",
667 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
668 "call_rcu_bh" => "call_rcu",
669 "rcu_barrier_bh" => "rcu_barrier",
670 "synchronize_sched" => "synchronize_rcu",
671 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
672 "call_rcu_sched" => "call_rcu",
673 "rcu_barrier_sched" => "rcu_barrier",
674 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
675 "cond_synchronize_sched" => "cond_synchronize_rcu",
676);
677
678#Create a search pattern for all these strings to speed up a loop below
679our $deprecated_apis_search = "";
680foreach my $entry (keys %deprecated_apis) {
681 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
682 $deprecated_apis_search .= $entry;
683}
684$deprecated_apis_search = "(?:${deprecated_apis_search})";
685
b392c64f
JP
686our $mode_perms_world_writable = qr{
687 S_IWUGO |
688 S_IWOTH |
689 S_IRWXUGO |
690 S_IALLUGO |
691 0[0-7][0-7][2367]
692}x;
693
f90774e1
JP
694our %mode_permission_string_types = (
695 "S_IRWXU" => 0700,
696 "S_IRUSR" => 0400,
697 "S_IWUSR" => 0200,
698 "S_IXUSR" => 0100,
699 "S_IRWXG" => 0070,
700 "S_IRGRP" => 0040,
701 "S_IWGRP" => 0020,
702 "S_IXGRP" => 0010,
703 "S_IRWXO" => 0007,
704 "S_IROTH" => 0004,
705 "S_IWOTH" => 0002,
706 "S_IXOTH" => 0001,
707 "S_IRWXUGO" => 0777,
708 "S_IRUGO" => 0444,
709 "S_IWUGO" => 0222,
710 "S_IXUGO" => 0111,
711);
712
713#Create a search pattern for all these strings to speed up a loop below
714our $mode_perms_string_search = "";
715foreach my $entry (keys %mode_permission_string_types) {
716 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
717 $mode_perms_string_search .= $entry;
718}
00180468
JP
719our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
720our $multi_mode_perms_string_search = qr{
721 ${single_mode_perms_string_search}
722 (?:\s*\|\s*${single_mode_perms_string_search})*
723}x;
724
725sub perms_to_octal {
726 my ($string) = @_;
727
728 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
729
730 my $val = "";
731 my $oval = "";
732 my $to = 0;
733 my $curpos = 0;
734 my $lastpos = 0;
735 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
736 $curpos = pos($string);
737 my $match = $2;
738 my $omatch = $1;
739 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
740 $lastpos = $curpos;
741 $to |= $mode_permission_string_types{$match};
742 $val .= '\s*\|\s*' if ($val ne "");
743 $val .= $match;
744 $oval .= $omatch;
745 }
746 $oval =~ s/^\s*\|\s*//;
747 $oval =~ s/\s*\|\s*$//;
748 return sprintf("%04o", $to);
749}
f90774e1 750
7840a94c
WS
751our $allowed_asm_includes = qr{(?x:
752 irq|
cdcee686
SR
753 memory|
754 time|
755 reboot
7840a94c
WS
756)};
757# memory.h: ARM has a custom one
758
66b47b4a
KC
759# Load common spelling mistakes and build regular expression list.
760my $misspellings;
66b47b4a 761my %spelling_fix;
66b47b4a 762
36061e38 763if (open(my $spelling, '<', $spelling_file)) {
36061e38
JP
764 while (<$spelling>) {
765 my $line = $_;
66b47b4a 766
36061e38
JP
767 $line =~ s/\s*\n?$//g;
768 $line =~ s/^\s*//g;
66b47b4a 769
36061e38
JP
770 next if ($line =~ m/^\s*#/);
771 next if ($line =~ m/^\s*$/);
772
773 my ($suspect, $fix) = split(/\|\|/, $line);
66b47b4a 774
36061e38
JP
775 $spelling_fix{$suspect} = $fix;
776 }
777 close($spelling);
36061e38
JP
778} else {
779 warn "No typos will be found - file '$spelling_file': $!\n";
66b47b4a 780}
66b47b4a 781
ebfd7d62
JP
782if ($codespell) {
783 if (open(my $spelling, '<', $codespellfile)) {
784 while (<$spelling>) {
785 my $line = $_;
786
787 $line =~ s/\s*\n?$//g;
788 $line =~ s/^\s*//g;
789
790 next if ($line =~ m/^\s*#/);
791 next if ($line =~ m/^\s*$/);
792 next if ($line =~ m/, disabled/i);
793
794 $line =~ s/,.*$//;
795
796 my ($suspect, $fix) = split(/->/, $line);
797
798 $spelling_fix{$suspect} = $fix;
799 }
800 close($spelling);
801 } else {
802 warn "No codespell typos will be found - file '$codespellfile': $!\n";
803 }
804}
805
806$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
807
75ad8c57
JF
808sub read_words {
809 my ($wordsRef, $file) = @_;
bf1fa1da 810
75ad8c57
JF
811 if (open(my $words, '<', $file)) {
812 while (<$words>) {
813 my $line = $_;
bf1fa1da 814
75ad8c57
JF
815 $line =~ s/\s*\n?$//g;
816 $line =~ s/^\s*//g;
bf1fa1da 817
75ad8c57
JF
818 next if ($line =~ m/^\s*#/);
819 next if ($line =~ m/^\s*$/);
820 if ($line =~ /\s/) {
821 print("$file: '$line' invalid - ignored\n");
822 next;
823 }
824
ced69da1 825 $$wordsRef .= '|' if (defined $$wordsRef);
75ad8c57
JF
826 $$wordsRef .= $line;
827 }
828 close($file);
829 return 1;
bf1fa1da 830 }
75ad8c57
JF
831
832 return 0;
833}
834
ced69da1
QM
835my $const_structs;
836if (show_type("CONST_STRUCT")) {
837 read_words(\$const_structs, $conststructsfile)
838 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
839}
75ad8c57 840
ced69da1
QM
841if (defined($typedefsfile)) {
842 my $typeOtherTypedefs;
75ad8c57
JF
843 read_words(\$typeOtherTypedefs, $typedefsfile)
844 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
ced69da1 845 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
bf1fa1da
JP
846}
847
8905a67c 848sub build_types {
485ff23e
AD
849 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
850 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
1813087d 851 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
8716de38 852 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
c8cb2ca3 853 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
ab7e23f3 854 $BasicType = qr{
ab7e23f3
JP
855 (?:$typeTypedefs\b)|
856 (?:${all}\b)
857 }x;
8905a67c 858 $NonptrType = qr{
d2172eb5 859 (?:$Modifier\s+|const\s+)*
cf655043 860 (?:
6b48db24 861 (?:typeof|__typeof__)\s*\([^\)]*\)|
8ed22cad 862 (?:$typeTypedefs\b)|
c45dcabd 863 (?:${all}\b)
cf655043 864 )
c8cb2ca3 865 (?:\s+$Modifier|\s+const)*
8905a67c 866 }x;
1813087d
JP
867 $NonptrTypeMisordered = qr{
868 (?:$Modifier\s+|const\s+)*
869 (?:
870 (?:${Misordered}\b)
871 )
872 (?:\s+$Modifier|\s+const)*
873 }x;
8716de38
JP
874 $NonptrTypeWithAttr = qr{
875 (?:$Modifier\s+|const\s+)*
876 (?:
877 (?:typeof|__typeof__)\s*\([^\)]*\)|
878 (?:$typeTypedefs\b)|
879 (?:${allWithAttr}\b)
880 )
881 (?:\s+$Modifier|\s+const)*
882 }x;
8905a67c 883 $Type = qr{
c45dcabd 884 $NonptrType
7b18496c 885 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
c8cb2ca3 886 (?:\s+$Inline|\s+$Modifier)*
8905a67c 887 }x;
1813087d
JP
888 $TypeMisordered = qr{
889 $NonptrTypeMisordered
7b18496c 890 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
1813087d
JP
891 (?:\s+$Inline|\s+$Modifier)*
892 }x;
91cb5195 893 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
1813087d 894 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8905a67c
AW
895}
896build_types();
6c72ffaa 897
7d2367af 898our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
d1fe9c09
JP
899
900# Using $balanced_parens, $LvalOrFunc, or $FuncArg
901# requires at least perl version v5.10.0
902# Any use must be runtime checked with $^V
903
904our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
2435880f 905our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
c0a5c898 906our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
7d2367af 907
f8422308 908our $declaration_macros = qr{(?x:
3e838b6c 909 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
fe658f94 910 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
3d102fc0 911 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
f8422308
JP
912)};
913
8d0325cc
AS
914our %allow_repeated_words = (
915 add => '',
916 added => '',
917 bad => '',
918 be => '',
919);
920
7d2367af
JP
921sub deparenthesize {
922 my ($string) = @_;
923 return "" if (!defined($string));
5b9553ab
JP
924
925 while ($string =~ /^\s*\(.*\)\s*$/) {
926 $string =~ s@^\s*\(\s*@@;
927 $string =~ s@\s*\)\s*$@@;
928 }
929
7d2367af 930 $string =~ s@\s+@ @g;
5b9553ab 931
7d2367af
JP
932 return $string;
933}
934
3445686a
JP
935sub seed_camelcase_file {
936 my ($file) = @_;
937
938 return if (!(-f $file));
939
940 local $/;
941
942 open(my $include_file, '<', "$file")
943 or warn "$P: Can't read '$file' $!\n";
944 my $text = <$include_file>;
945 close($include_file);
946
947 my @lines = split('\n', $text);
948
949 foreach my $line (@lines) {
950 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
951 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
952 $camelcase{$1} = 1;
11ea516a
JP
953 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
954 $camelcase{$1} = 1;
955 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
3445686a
JP
956 $camelcase{$1} = 1;
957 }
958 }
959}
960
cd28b119
JP
961our %maintained_status = ();
962
85b0ee18
JP
963sub is_maintained_obsolete {
964 my ($filename) = @_;
965
f2c19c2f 966 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
85b0ee18 967
cd28b119
JP
968 if (!exists($maintained_status{$filename})) {
969 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
970 }
85b0ee18 971
cd28b119 972 return $maintained_status{$filename} =~ /obsolete/i;
85b0ee18
JP
973}
974
3b6e8ac9
JP
975sub is_SPDX_License_valid {
976 my ($license) = @_;
977
0f7f635b 978 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
3b6e8ac9 979
56294112
JP
980 my $root_path = abs_path($root);
981 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
3b6e8ac9
JP
982 return 0 if ($status ne "");
983 return 1;
984}
985
3445686a
JP
986my $camelcase_seeded = 0;
987sub seed_camelcase_includes {
988 return if ($camelcase_seeded);
989
990 my $files;
c707a81d
JP
991 my $camelcase_cache = "";
992 my @include_files = ();
993
994 $camelcase_seeded = 1;
351b2a1f 995
0f7f635b 996 if (-e "$gitroot") {
dbbf869d 997 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
351b2a1f 998 chomp $git_last_include_commit;
c707a81d 999 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
3445686a 1000 } else {
c707a81d 1001 my $last_mod_date = 0;
3445686a 1002 $files = `find $root/include -name "*.h"`;
c707a81d
JP
1003 @include_files = split('\n', $files);
1004 foreach my $file (@include_files) {
1005 my $date = POSIX::strftime("%Y%m%d%H%M",
1006 localtime((stat $file)[9]));
1007 $last_mod_date = $date if ($last_mod_date < $date);
1008 }
1009 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1010 }
1011
1012 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1013 open(my $camelcase_file, '<', "$camelcase_cache")
1014 or warn "$P: Can't read '$camelcase_cache' $!\n";
1015 while (<$camelcase_file>) {
1016 chomp;
1017 $camelcase{$_} = 1;
1018 }
1019 close($camelcase_file);
1020
1021 return;
3445686a 1022 }
c707a81d 1023
0f7f635b 1024 if (-e "$gitroot") {
dbbf869d 1025 $files = `${git_command} ls-files "include/*.h"`;
c707a81d
JP
1026 @include_files = split('\n', $files);
1027 }
1028
3445686a
JP
1029 foreach my $file (@include_files) {
1030 seed_camelcase_file($file);
1031 }
351b2a1f 1032
c707a81d 1033 if ($camelcase_cache ne "") {
351b2a1f 1034 unlink glob ".checkpatch-camelcase.*";
c707a81d
JP
1035 open(my $camelcase_file, '>', "$camelcase_cache")
1036 or warn "$P: Can't write '$camelcase_cache' $!\n";
351b2a1f
JP
1037 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1038 print $camelcase_file ("$_\n");
1039 }
1040 close($camelcase_file);
1041 }
3445686a
JP
1042}
1043
f5f61325
JP
1044sub git_is_single_file {
1045 my ($filename) = @_;
1046
1047 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1048
1049 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1050 my $count = $output =~ tr/\n//;
1051 return $count eq 1 && $output =~ m{^${filename}$};
1052}
1053
d311cd44
JP
1054sub git_commit_info {
1055 my ($commit, $id, $desc) = @_;
1056
0f7f635b 1057 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
d311cd44 1058
dbbf869d 1059 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
d311cd44
JP
1060 $output =~ s/^\s*//gm;
1061 my @lines = split("\n", $output);
1062
0d7835fc
JP
1063 return ($id, $desc) if ($#lines < 0);
1064
5a7f4455 1065 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
d311cd44
JP
1066# Maybe one day convert this block of bash into something that returns
1067# all matching commit ids, but it's very slow...
1068#
1069# echo "checking commits $1..."
1070# git rev-list --remotes | grep -i "^$1" |
1071# while read line ; do
1072# git log --format='%H %s' -1 $line |
1073# echo "commit $(cut -c 1-12,41-)"
1074# done
1075 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
948b133a 1076 $id = undef;
d311cd44
JP
1077 } else {
1078 $id = substr($lines[0], 0, 12);
1079 $desc = substr($lines[0], 41);
1080 }
1081
1082 return ($id, $desc);
1083}
1084
6c72ffaa
AW
1085$chk_signoff = 0 if ($file);
1086
00df344f 1087my @rawlines = ();
c2fdda0d 1088my @lines = ();
3705ce5b 1089my @fixed = ();
d752fcc8
JP
1090my @fixed_inserted = ();
1091my @fixed_deleted = ();
194f66fc
JP
1092my $fixlinenr = -1;
1093
4a593c34
CD
1094# If input is git commits, extract all commits from the commit expressions.
1095# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
0f7f635b 1096die "$P: No git repository found\n" if ($git && !-e "$gitroot");
4a593c34
CD
1097
1098if ($git) {
1099 my @commits = ();
0dea9f1e 1100 foreach my $commit_expr (@ARGV) {
4a593c34 1101 my $git_range;
28898fd1
JP
1102 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1103 $git_range = "-$2 $1";
4a593c34
CD
1104 } elsif ($commit_expr =~ m/\.\./) {
1105 $git_range = "$commit_expr";
4a593c34 1106 } else {
0dea9f1e
JP
1107 $git_range = "-1 $commit_expr";
1108 }
dbbf869d 1109 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
0dea9f1e 1110 foreach my $line (split(/\n/, $lines)) {
28898fd1
JP
1111 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1112 next if (!defined($1) || !defined($2));
0dea9f1e
JP
1113 my $sha1 = $1;
1114 my $subject = $2;
1115 unshift(@commits, $sha1);
1116 $git_commits{$sha1} = $subject;
4a593c34
CD
1117 }
1118 }
1119 die "$P: no git commits after extraction!\n" if (@commits == 0);
1120 @ARGV = @commits;
1121}
1122
c2fdda0d 1123my $vname;
98005e8c 1124$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
6c72ffaa 1125for my $filename (@ARGV) {
21caa13c 1126 my $FILE;
f5f61325
JP
1127 my $is_git_file = git_is_single_file($filename);
1128 my $oldfile = $file;
1129 $file = 1 if ($is_git_file);
4a593c34
CD
1130 if ($git) {
1131 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1132 die "$P: $filename: git format-patch failed - $!\n";
1133 } elsif ($file) {
21caa13c 1134 open($FILE, '-|', "diff -u /dev/null $filename") ||
6c72ffaa 1135 die "$P: $filename: diff failed - $!\n";
21caa13c
AW
1136 } elsif ($filename eq '-') {
1137 open($FILE, '<&STDIN');
6c72ffaa 1138 } else {
21caa13c 1139 open($FILE, '<', "$filename") ||
6c72ffaa 1140 die "$P: $filename: open failed - $!\n";
0a920b5b 1141 }
c2fdda0d
AW
1142 if ($filename eq '-') {
1143 $vname = 'Your patch';
4a593c34 1144 } elsif ($git) {
0dea9f1e 1145 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
c2fdda0d
AW
1146 } else {
1147 $vname = $filename;
1148 }
21caa13c 1149 while (<$FILE>) {
6c72ffaa
AW
1150 chomp;
1151 push(@rawlines, $_);
c7f574d0 1152 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
6c72ffaa 1153 }
21caa13c 1154 close($FILE);
d8469f16
JP
1155
1156 if ($#ARGV > 0 && $quiet == 0) {
1157 print '-' x length($vname) . "\n";
1158 print "$vname\n";
1159 print '-' x length($vname) . "\n";
1160 }
1161
c2fdda0d 1162 if (!process($filename)) {
6c72ffaa
AW
1163 $exit = 1;
1164 }
1165 @rawlines = ();
13214adf 1166 @lines = ();
3705ce5b 1167 @fixed = ();
d752fcc8
JP
1168 @fixed_inserted = ();
1169 @fixed_deleted = ();
194f66fc 1170 $fixlinenr = -1;
485ff23e
AD
1171 @modifierListFile = ();
1172 @typeListFile = ();
1173 build_types();
f5f61325 1174 $file = $oldfile if ($is_git_file);
0a920b5b
AW
1175}
1176
d8469f16 1177if (!$quiet) {
3c816e49
JP
1178 hash_show_words(\%use_type, "Used");
1179 hash_show_words(\%ignore_type, "Ignored");
1180
5b57980d 1181 if (!$perl_version_ok) {
d8469f16
JP
1182 print << "EOM"
1183
1184NOTE: perl $^V is not modern enough to detect all possible issues.
5b57980d 1185 An upgrade to at least perl $minimum_perl_version is suggested.
d8469f16
JP
1186EOM
1187 }
1188 if ($exit) {
1189 print << "EOM"
1190
1191NOTE: If any of the errors are false positives, please report
1192 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1193EOM
1194 }
1195}
1196
0a920b5b
AW
1197exit($exit);
1198
1199sub top_of_kernel_tree {
6c72ffaa
AW
1200 my ($root) = @_;
1201
1202 my @tree_check = (
1203 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1204 "README", "Documentation", "arch", "include", "drivers",
1205 "fs", "init", "ipc", "kernel", "lib", "scripts",
1206 );
1207
1208 foreach my $check (@tree_check) {
1209 if (! -e $root . '/' . $check) {
1210 return 0;
1211 }
0a920b5b 1212 }
6c72ffaa 1213 return 1;
8f26b837 1214}
0a920b5b 1215
20112475
JP
1216sub parse_email {
1217 my ($formatted_email) = @_;
1218
1219 my $name = "";
fccaebf0 1220 my $quoted = "";
dfa05c28 1221 my $name_comment = "";
20112475
JP
1222 my $address = "";
1223 my $comment = "";
1224
1225 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1226 $name = $1;
1227 $address = $2;
1228 $comment = $3 if defined $3;
1229 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1230 $address = $1;
1231 $comment = $2 if defined $2;
1232 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1233 $address = $1;
1234 $comment = $2 if defined $2;
85e12066 1235 $formatted_email =~ s/\Q$address\E.*$//;
20112475 1236 $name = $formatted_email;
3705ce5b 1237 $name = trim($name);
20112475
JP
1238 $name =~ s/^\"|\"$//g;
1239 # If there's a name left after stripping spaces and
1240 # leading quotes, and the address doesn't have both
1241 # leading and trailing angle brackets, the address
1242 # is invalid. ie:
1243 # "joe smith joe@smith.com" bad
1244 # "joe smith <joe@smith.com" bad
1245 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1246 $name = "";
1247 $address = "";
1248 $comment = "";
1249 }
1250 }
1251
fccaebf0
DR
1252 # Extract comments from names excluding quoted parts
1253 # "John D. (Doe)" - Do not extract
1254 if ($name =~ s/\"(.+)\"//) {
1255 $quoted = $1;
1256 }
1257 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1258 $name_comment .= trim($1);
dfa05c28 1259 }
fccaebf0
DR
1260 $name =~ s/^[ \"]+|[ \"]+$//g;
1261 $name = trim("$quoted $name");
1262
3705ce5b 1263 $address = trim($address);
20112475 1264 $address =~ s/^\<|\>$//g;
fccaebf0 1265 $comment = trim($comment);
20112475
JP
1266
1267 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1268 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1269 $name = "\"$name\"";
1270 }
1271
dfa05c28 1272 return ($name, $name_comment, $address, $comment);
20112475
JP
1273}
1274
1275sub format_email {
48ca2d8a 1276 my ($name, $name_comment, $address, $comment) = @_;
20112475
JP
1277
1278 my $formatted_email;
1279
fccaebf0 1280 $name =~ s/^[ \"]+|[ \"]+$//g;
3705ce5b 1281 $address = trim($address);
fccaebf0 1282 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
20112475
JP
1283
1284 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1285 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1286 $name = "\"$name\"";
1287 }
1288
fccaebf0
DR
1289 $name_comment = trim($name_comment);
1290 $name_comment = " $name_comment" if ($name_comment ne "");
1291 $comment = trim($comment);
1292 $comment = " $comment" if ($comment ne "");
1293
20112475
JP
1294 if ("$name" eq "") {
1295 $formatted_email = "$address";
1296 } else {
48ca2d8a 1297 $formatted_email = "$name$name_comment <$address>";
20112475 1298 }
48ca2d8a 1299 $formatted_email .= "$comment";
20112475
JP
1300 return $formatted_email;
1301}
1302
dfa05c28
JP
1303sub reformat_email {
1304 my ($email) = @_;
1305
1306 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
48ca2d8a 1307 return format_email($email_name, $name_comment, $email_address, $comment);
dfa05c28
JP
1308}
1309
1310sub same_email_addresses {
fccaebf0 1311 my ($email1, $email2) = @_;
dfa05c28
JP
1312
1313 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1314 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1315
1316 return $email1_name eq $email2_name &&
48ca2d8a
DR
1317 $email1_address eq $email2_address &&
1318 $name1_comment eq $name2_comment &&
1319 $comment1 eq $comment2;
dfa05c28
JP
1320}
1321
d311cd44 1322sub which {
bd474ca0 1323 my ($bin) = @_;
d311cd44 1324
bd474ca0
JP
1325 foreach my $path (split(/:/, $ENV{PATH})) {
1326 if (-e "$path/$bin") {
1327 return "$path/$bin";
1328 }
d311cd44 1329 }
d311cd44 1330
bd474ca0 1331 return "";
d311cd44
JP
1332}
1333
000d1cc1
JP
1334sub which_conf {
1335 my ($conf) = @_;
1336
1337 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1338 if (-e "$path/$conf") {
1339 return "$path/$conf";
1340 }
1341 }
1342
1343 return "";
1344}
1345
0a920b5b
AW
1346sub expand_tabs {
1347 my ($str) = @_;
1348
1349 my $res = '';
1350 my $n = 0;
1351 for my $c (split(//, $str)) {
1352 if ($c eq "\t") {
1353 $res .= ' ';
1354 $n++;
713a09de 1355 for (; ($n % $tabsize) != 0; $n++) {
0a920b5b
AW
1356 $res .= ' ';
1357 }
1358 next;
1359 }
1360 $res .= $c;
1361 $n++;
1362 }
1363
1364 return $res;
1365}
6c72ffaa 1366sub copy_spacing {
773647a0 1367 (my $res = shift) =~ tr/\t/ /c;
6c72ffaa
AW
1368 return $res;
1369}
0a920b5b 1370
4a0df2ef
AW
1371sub line_stats {
1372 my ($line) = @_;
1373
1374 # Drop the diff line leader and expand tabs
1375 $line =~ s/^.//;
1376 $line = expand_tabs($line);
1377
1378 # Pick the indent from the front of the line.
1379 my ($white) = ($line =~ /^(\s*)/);
1380
1381 return (length($line), length($white));
1382}
1383
773647a0
AW
1384my $sanitise_quote = '';
1385
1386sub sanitise_line_reset {
1387 my ($in_comment) = @_;
1388
1389 if ($in_comment) {
1390 $sanitise_quote = '*/';
1391 } else {
1392 $sanitise_quote = '';
1393 }
1394}
00df344f
AW
1395sub sanitise_line {
1396 my ($line) = @_;
1397
1398 my $res = '';
1399 my $l = '';
1400
c2fdda0d 1401 my $qlen = 0;
773647a0
AW
1402 my $off = 0;
1403 my $c;
00df344f 1404
773647a0
AW
1405 # Always copy over the diff marker.
1406 $res = substr($line, 0, 1);
1407
1408 for ($off = 1; $off < length($line); $off++) {
1409 $c = substr($line, $off, 1);
1410
8d2e11b2 1411 # Comments we are whacking completely including the begin
773647a0
AW
1412 # and end, all to $;.
1413 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1414 $sanitise_quote = '*/';
1415
1416 substr($res, $off, 2, "$;$;");
1417 $off++;
1418 next;
00df344f 1419 }
81bc0e02 1420 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
773647a0
AW
1421 $sanitise_quote = '';
1422 substr($res, $off, 2, "$;$;");
1423 $off++;
1424 next;
c2fdda0d 1425 }
113f04a8
DW
1426 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1427 $sanitise_quote = '//';
1428
1429 substr($res, $off, 2, $sanitise_quote);
1430 $off++;
1431 next;
1432 }
773647a0
AW
1433
1434 # A \ in a string means ignore the next character.
1435 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1436 $c eq "\\") {
1437 substr($res, $off, 2, 'XX');
1438 $off++;
1439 next;
00df344f 1440 }
773647a0
AW
1441 # Regular quotes.
1442 if ($c eq "'" || $c eq '"') {
1443 if ($sanitise_quote eq '') {
1444 $sanitise_quote = $c;
00df344f 1445
773647a0
AW
1446 substr($res, $off, 1, $c);
1447 next;
1448 } elsif ($sanitise_quote eq $c) {
1449 $sanitise_quote = '';
1450 }
1451 }
00df344f 1452
fae17dae 1453 #print "c<$c> SQ<$sanitise_quote>\n";
773647a0
AW
1454 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1455 substr($res, $off, 1, $;);
113f04a8
DW
1456 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1457 substr($res, $off, 1, $;);
773647a0
AW
1458 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1459 substr($res, $off, 1, 'X');
1460 } else {
1461 substr($res, $off, 1, $c);
1462 }
c2fdda0d
AW
1463 }
1464
113f04a8
DW
1465 if ($sanitise_quote eq '//') {
1466 $sanitise_quote = '';
1467 }
1468
c2fdda0d 1469 # The pathname on a #include may be surrounded by '<' and '>'.
c45dcabd 1470 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
c2fdda0d
AW
1471 my $clean = 'X' x length($1);
1472 $res =~ s@\<.*\>@<$clean>@;
1473
1474 # The whole of a #error is a string.
c45dcabd 1475 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
c2fdda0d 1476 my $clean = 'X' x length($1);
c45dcabd 1477 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
c2fdda0d
AW
1478 }
1479
dadf680d
JP
1480 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1481 my $match = $1;
1482 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1483 }
1484
00df344f
AW
1485 return $res;
1486}
1487
a6962d72
JP
1488sub get_quoted_string {
1489 my ($line, $rawline) = @_;
1490
478b1799 1491 return "" if (!defined($line) || !defined($rawline));
33acb54a 1492 return "" if ($line !~ m/($String)/g);
a6962d72
JP
1493 return substr($rawline, $-[0], $+[0] - $-[0]);
1494}
1495
8905a67c
AW
1496sub ctx_statement_block {
1497 my ($linenr, $remain, $off) = @_;
1498 my $line = $linenr - 1;
1499 my $blk = '';
1500 my $soff = $off;
1501 my $coff = $off - 1;
773647a0 1502 my $coff_set = 0;
8905a67c 1503
13214adf
AW
1504 my $loff = 0;
1505
8905a67c
AW
1506 my $type = '';
1507 my $level = 0;
a2750645 1508 my @stack = ();
cf655043 1509 my $p;
8905a67c
AW
1510 my $c;
1511 my $len = 0;
13214adf
AW
1512
1513 my $remainder;
8905a67c 1514 while (1) {
a2750645
AW
1515 @stack = (['', 0]) if ($#stack == -1);
1516
773647a0 1517 #warn "CSB: blk<$blk> remain<$remain>\n";
8905a67c
AW
1518 # If we are about to drop off the end, pull in more
1519 # context.
1520 if ($off >= $len) {
1521 for (; $remain > 0; $line++) {
dea33496 1522 last if (!defined $lines[$line]);
c2fdda0d 1523 next if ($lines[$line] =~ /^-/);
8905a67c 1524 $remain--;
13214adf 1525 $loff = $len;
c2fdda0d 1526 $blk .= $lines[$line] . "\n";
8905a67c
AW
1527 $len = length($blk);
1528 $line++;
1529 last;
1530 }
1531 # Bail if there is no further context.
1532 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
13214adf 1533 if ($off >= $len) {
8905a67c
AW
1534 last;
1535 }
f74bd194
AW
1536 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1537 $level++;
1538 $type = '#';
1539 }
8905a67c 1540 }
cf655043 1541 $p = $c;
8905a67c 1542 $c = substr($blk, $off, 1);
13214adf 1543 $remainder = substr($blk, $off);
8905a67c 1544
773647a0 1545 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4635f4fb
AW
1546
1547 # Handle nested #if/#else.
1548 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1549 push(@stack, [ $type, $level ]);
1550 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1551 ($type, $level) = @{$stack[$#stack - 1]};
1552 } elsif ($remainder =~ /^#\s*endif\b/) {
1553 ($type, $level) = @{pop(@stack)};
1554 }
1555
8905a67c
AW
1556 # Statement ends at the ';' or a close '}' at the
1557 # outermost level.
1558 if ($level == 0 && $c eq ';') {
1559 last;
1560 }
1561
13214adf 1562 # An else is really a conditional as long as its not else if
773647a0
AW
1563 if ($level == 0 && $coff_set == 0 &&
1564 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1565 $remainder =~ /^(else)(?:\s|{)/ &&
1566 $remainder !~ /^else\s+if\b/) {
1567 $coff = $off + length($1) - 1;
1568 $coff_set = 1;
1569 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1570 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
13214adf
AW
1571 }
1572
8905a67c
AW
1573 if (($type eq '' || $type eq '(') && $c eq '(') {
1574 $level++;
1575 $type = '(';
1576 }
1577 if ($type eq '(' && $c eq ')') {
1578 $level--;
1579 $type = ($level != 0)? '(' : '';
1580
1581 if ($level == 0 && $coff < $soff) {
1582 $coff = $off;
773647a0
AW
1583 $coff_set = 1;
1584 #warn "CSB: mark coff<$coff>\n";
8905a67c
AW
1585 }
1586 }
1587 if (($type eq '' || $type eq '{') && $c eq '{') {
1588 $level++;
1589 $type = '{';
1590 }
1591 if ($type eq '{' && $c eq '}') {
1592 $level--;
1593 $type = ($level != 0)? '{' : '';
1594
1595 if ($level == 0) {
b998e001
PP
1596 if (substr($blk, $off + 1, 1) eq ';') {
1597 $off++;
1598 }
8905a67c
AW
1599 last;
1600 }
1601 }
f74bd194
AW
1602 # Preprocessor commands end at the newline unless escaped.
1603 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1604 $level--;
1605 $type = '';
1606 $off++;
1607 last;
1608 }
8905a67c
AW
1609 $off++;
1610 }
a3bb97a7 1611 # We are truly at the end, so shuffle to the next line.
13214adf 1612 if ($off == $len) {
a3bb97a7 1613 $loff = $len + 1;
13214adf
AW
1614 $line++;
1615 $remain--;
1616 }
8905a67c
AW
1617
1618 my $statement = substr($blk, $soff, $off - $soff + 1);
1619 my $condition = substr($blk, $soff, $coff - $soff + 1);
1620
1621 #warn "STATEMENT<$statement>\n";
1622 #warn "CONDITION<$condition>\n";
1623
773647a0 1624 #print "coff<$coff> soff<$off> loff<$loff>\n";
13214adf
AW
1625
1626 return ($statement, $condition,
1627 $line, $remain + 1, $off - $loff + 1, $level);
1628}
1629
cf655043
AW
1630sub statement_lines {
1631 my ($stmt) = @_;
1632
1633 # Strip the diff line prefixes and rip blank lines at start and end.
1634 $stmt =~ s/(^|\n)./$1/g;
1635 $stmt =~ s/^\s*//;
1636 $stmt =~ s/\s*$//;
1637
1638 my @stmt_lines = ($stmt =~ /\n/g);
1639
1640 return $#stmt_lines + 2;
1641}
1642
1643sub statement_rawlines {
1644 my ($stmt) = @_;
1645
1646 my @stmt_lines = ($stmt =~ /\n/g);
1647
1648 return $#stmt_lines + 2;
1649}
1650
1651sub statement_block_size {
1652 my ($stmt) = @_;
1653
1654 $stmt =~ s/(^|\n)./$1/g;
1655 $stmt =~ s/^\s*{//;
1656 $stmt =~ s/}\s*$//;
1657 $stmt =~ s/^\s*//;
1658 $stmt =~ s/\s*$//;
1659
1660 my @stmt_lines = ($stmt =~ /\n/g);
1661 my @stmt_statements = ($stmt =~ /;/g);
1662
1663 my $stmt_lines = $#stmt_lines + 2;
1664 my $stmt_statements = $#stmt_statements + 1;
1665
1666 if ($stmt_lines > $stmt_statements) {
1667 return $stmt_lines;
1668 } else {
1669 return $stmt_statements;
1670 }
1671}
1672
13214adf
AW
1673sub ctx_statement_full {
1674 my ($linenr, $remain, $off) = @_;
1675 my ($statement, $condition, $level);
1676
1677 my (@chunks);
1678
cf655043 1679 # Grab the first conditional/block pair.
13214adf
AW
1680 ($statement, $condition, $linenr, $remain, $off, $level) =
1681 ctx_statement_block($linenr, $remain, $off);
773647a0 1682 #print "F: c<$condition> s<$statement> remain<$remain>\n";
cf655043
AW
1683 push(@chunks, [ $condition, $statement ]);
1684 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1685 return ($level, $linenr, @chunks);
1686 }
1687
1688 # Pull in the following conditional/block pairs and see if they
1689 # could continue the statement.
13214adf 1690 for (;;) {
13214adf
AW
1691 ($statement, $condition, $linenr, $remain, $off, $level) =
1692 ctx_statement_block($linenr, $remain, $off);
cf655043 1693 #print "C: c<$condition> s<$statement> remain<$remain>\n";
773647a0 1694 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
cf655043
AW
1695 #print "C: push\n";
1696 push(@chunks, [ $condition, $statement ]);
13214adf
AW
1697 }
1698
1699 return ($level, $linenr, @chunks);
8905a67c
AW
1700}
1701
4a0df2ef 1702sub ctx_block_get {
f0a594c1 1703 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
4a0df2ef
AW
1704 my $line;
1705 my $start = $linenr - 1;
4a0df2ef
AW
1706 my $blk = '';
1707 my @o;
1708 my @c;
1709 my @res = ();
1710
f0a594c1 1711 my $level = 0;
4635f4fb 1712 my @stack = ($level);
00df344f
AW
1713 for ($line = $start; $remain > 0; $line++) {
1714 next if ($rawlines[$line] =~ /^-/);
1715 $remain--;
1716
1717 $blk .= $rawlines[$line];
4635f4fb
AW
1718
1719 # Handle nested #if/#else.
01464f30 1720 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
4635f4fb 1721 push(@stack, $level);
01464f30 1722 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
4635f4fb 1723 $level = $stack[$#stack - 1];
01464f30 1724 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
4635f4fb
AW
1725 $level = pop(@stack);
1726 }
1727
01464f30 1728 foreach my $c (split(//, $lines[$line])) {
f0a594c1
AW
1729 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1730 if ($off > 0) {
1731 $off--;
1732 next;
1733 }
4a0df2ef 1734
f0a594c1
AW
1735 if ($c eq $close && $level > 0) {
1736 $level--;
1737 last if ($level == 0);
1738 } elsif ($c eq $open) {
1739 $level++;
1740 }
1741 }
4a0df2ef 1742
f0a594c1 1743 if (!$outer || $level <= 1) {
00df344f 1744 push(@res, $rawlines[$line]);
4a0df2ef
AW
1745 }
1746
f0a594c1 1747 last if ($level == 0);
4a0df2ef
AW
1748 }
1749
f0a594c1 1750 return ($level, @res);
4a0df2ef
AW
1751}
1752sub ctx_block_outer {
1753 my ($linenr, $remain) = @_;
1754
f0a594c1
AW
1755 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1756 return @r;
4a0df2ef
AW
1757}
1758sub ctx_block {
1759 my ($linenr, $remain) = @_;
1760
f0a594c1
AW
1761 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1762 return @r;
653d4876
AW
1763}
1764sub ctx_statement {
f0a594c1
AW
1765 my ($linenr, $remain, $off) = @_;
1766
1767 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1768 return @r;
1769}
1770sub ctx_block_level {
653d4876
AW
1771 my ($linenr, $remain) = @_;
1772
f0a594c1 1773 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
4a0df2ef 1774}
9c0ca6f9
AW
1775sub ctx_statement_level {
1776 my ($linenr, $remain, $off) = @_;
1777
1778 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1779}
4a0df2ef
AW
1780
1781sub ctx_locate_comment {
1782 my ($first_line, $end_line) = @_;
1783
a55ee0cc
JP
1784 # If c99 comment on the current line, or the line before or after
1785 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1786 return $current_comment if (defined $current_comment);
1787 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1788 return $current_comment if (defined $current_comment);
1789 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1790 return $current_comment if (defined $current_comment);
1791
4a0df2ef 1792 # Catch a comment on the end of the line itself.
a55ee0cc 1793 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
4a0df2ef
AW
1794 return $current_comment if (defined $current_comment);
1795
1796 # Look through the context and try and figure out if there is a
1797 # comment.
1798 my $in_comment = 0;
1799 $current_comment = '';
1800 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
00df344f
AW
1801 my $line = $rawlines[$linenr - 1];
1802 #warn " $line\n";
4a0df2ef
AW
1803 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1804 $in_comment = 1;
1805 }
1806 if ($line =~ m@/\*@) {
1807 $in_comment = 1;
1808 }
1809 if (!$in_comment && $current_comment ne '') {
1810 $current_comment = '';
1811 }
1812 $current_comment .= $line . "\n" if ($in_comment);
1813 if ($line =~ m@\*/@) {
1814 $in_comment = 0;
1815 }
1816 }
1817
1818 chomp($current_comment);
1819 return($current_comment);
1820}
1821sub ctx_has_comment {
1822 my ($first_line, $end_line) = @_;
1823 my $cmt = ctx_locate_comment($first_line, $end_line);
1824
00df344f 1825 ##print "LINE: $rawlines[$end_line - 1 ]\n";
4a0df2ef
AW
1826 ##print "CMMT: $cmt\n";
1827
1828 return ($cmt ne '');
1829}
1830
4d001e4d
AW
1831sub raw_line {
1832 my ($linenr, $cnt) = @_;
1833
1834 my $offset = $linenr - 1;
1835 $cnt++;
1836
1837 my $line;
1838 while ($cnt) {
1839 $line = $rawlines[$offset++];
1840 next if (defined($line) && $line =~ /^-/);
1841 $cnt--;
1842 }
1843
1844 return $line;
1845}
1846
2a9f9d85
TH
1847sub get_stat_real {
1848 my ($linenr, $lc) = @_;
1849
1850 my $stat_real = raw_line($linenr, 0);
1851 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1852 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1853 }
1854
1855 return $stat_real;
1856}
1857
e3d95a2a
TH
1858sub get_stat_here {
1859 my ($linenr, $cnt, $here) = @_;
1860
1861 my $herectx = $here . "\n";
1862 for (my $n = 0; $n < $cnt; $n++) {
1863 $herectx .= raw_line($linenr, $n) . "\n";
1864 }
1865
1866 return $herectx;
1867}
1868
6c72ffaa
AW
1869sub cat_vet {
1870 my ($vet) = @_;
1871 my ($res, $coded);
9c0ca6f9 1872
6c72ffaa
AW
1873 $res = '';
1874 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1875 $res .= $1;
1876 if ($2 ne '') {
1877 $coded = sprintf("^%c", unpack('C', $2) + 64);
1878 $res .= $coded;
9c0ca6f9
AW
1879 }
1880 }
6c72ffaa 1881 $res =~ s/$/\$/;
9c0ca6f9 1882
6c72ffaa 1883 return $res;
9c0ca6f9
AW
1884}
1885
c2fdda0d 1886my $av_preprocessor = 0;
cf655043 1887my $av_pending;
c2fdda0d 1888my @av_paren_type;
1f65f947 1889my $av_pend_colon;
c2fdda0d
AW
1890
1891sub annotate_reset {
1892 $av_preprocessor = 0;
cf655043
AW
1893 $av_pending = '_';
1894 @av_paren_type = ('E');
1f65f947 1895 $av_pend_colon = 'O';
c2fdda0d
AW
1896}
1897
6c72ffaa
AW
1898sub annotate_values {
1899 my ($stream, $type) = @_;
0a920b5b 1900
6c72ffaa 1901 my $res;
1f65f947 1902 my $var = '_' x length($stream);
6c72ffaa
AW
1903 my $cur = $stream;
1904
c2fdda0d 1905 print "$stream\n" if ($dbg_values > 1);
6c72ffaa 1906
6c72ffaa 1907 while (length($cur)) {
773647a0 1908 @av_paren_type = ('E') if ($#av_paren_type < 0);
cf655043 1909 print " <" . join('', @av_paren_type) .
171ae1a4 1910 "> <$type> <$av_pending>" if ($dbg_values > 1);
6c72ffaa 1911 if ($cur =~ /^(\s+)/o) {
c2fdda0d
AW
1912 print "WS($1)\n" if ($dbg_values > 1);
1913 if ($1 =~ /\n/ && $av_preprocessor) {
cf655043 1914 $type = pop(@av_paren_type);
c2fdda0d 1915 $av_preprocessor = 0;
6c72ffaa
AW
1916 }
1917
c023e473 1918 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
9446ef56
AW
1919 print "CAST($1)\n" if ($dbg_values > 1);
1920 push(@av_paren_type, $type);
addcdcea 1921 $type = 'c';
9446ef56 1922
e91b6e26 1923 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
c2fdda0d 1924 print "DECLARE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1925 $type = 'T';
1926
389a2fe5
AW
1927 } elsif ($cur =~ /^($Modifier)\s*/) {
1928 print "MODIFIER($1)\n" if ($dbg_values > 1);
1929 $type = 'T';
1930
c45dcabd 1931 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
171ae1a4 1932 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
c2fdda0d 1933 $av_preprocessor = 1;
171ae1a4
AW
1934 push(@av_paren_type, $type);
1935 if ($2 ne '') {
1936 $av_pending = 'N';
1937 }
1938 $type = 'E';
1939
c45dcabd 1940 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
171ae1a4
AW
1941 print "UNDEF($1)\n" if ($dbg_values > 1);
1942 $av_preprocessor = 1;
1943 push(@av_paren_type, $type);
6c72ffaa 1944
c45dcabd 1945 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
cf655043 1946 print "PRE_START($1)\n" if ($dbg_values > 1);
c2fdda0d 1947 $av_preprocessor = 1;
cf655043
AW
1948
1949 push(@av_paren_type, $type);
1950 push(@av_paren_type, $type);
171ae1a4 1951 $type = 'E';
cf655043 1952
c45dcabd 1953 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
cf655043
AW
1954 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1955 $av_preprocessor = 1;
1956
1957 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1958
171ae1a4 1959 $type = 'E';
cf655043 1960
c45dcabd 1961 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
cf655043
AW
1962 print "PRE_END($1)\n" if ($dbg_values > 1);
1963
1964 $av_preprocessor = 1;
1965
1966 # Assume all arms of the conditional end as this
1967 # one does, and continue as if the #endif was not here.
1968 pop(@av_paren_type);
1969 push(@av_paren_type, $type);
171ae1a4 1970 $type = 'E';
6c72ffaa
AW
1971
1972 } elsif ($cur =~ /^(\\\n)/o) {
c2fdda0d 1973 print "PRECONT($1)\n" if ($dbg_values > 1);
6c72ffaa 1974
171ae1a4
AW
1975 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1976 print "ATTR($1)\n" if ($dbg_values > 1);
1977 $av_pending = $type;
1978 $type = 'N';
1979
6c72ffaa 1980 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
c2fdda0d 1981 print "SIZEOF($1)\n" if ($dbg_values > 1);
6c72ffaa 1982 if (defined $2) {
cf655043 1983 $av_pending = 'V';
6c72ffaa
AW
1984 }
1985 $type = 'N';
1986
14b111c1 1987 } elsif ($cur =~ /^(if|while|for)\b/o) {
c2fdda0d 1988 print "COND($1)\n" if ($dbg_values > 1);
14b111c1 1989 $av_pending = 'E';
6c72ffaa
AW
1990 $type = 'N';
1991
1f65f947
AW
1992 } elsif ($cur =~/^(case)/o) {
1993 print "CASE($1)\n" if ($dbg_values > 1);
1994 $av_pend_colon = 'C';
1995 $type = 'N';
1996
14b111c1 1997 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
c2fdda0d 1998 print "KEYWORD($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1999 $type = 'N';
2000
2001 } elsif ($cur =~ /^(\()/o) {
c2fdda0d 2002 print "PAREN('$1')\n" if ($dbg_values > 1);
cf655043
AW
2003 push(@av_paren_type, $av_pending);
2004 $av_pending = '_';
6c72ffaa
AW
2005 $type = 'N';
2006
2007 } elsif ($cur =~ /^(\))/o) {
cf655043
AW
2008 my $new_type = pop(@av_paren_type);
2009 if ($new_type ne '_') {
2010 $type = $new_type;
c2fdda0d
AW
2011 print "PAREN('$1') -> $type\n"
2012 if ($dbg_values > 1);
6c72ffaa 2013 } else {
c2fdda0d 2014 print "PAREN('$1')\n" if ($dbg_values > 1);
6c72ffaa
AW
2015 }
2016
c8cb2ca3 2017 } elsif ($cur =~ /^($Ident)\s*\(/o) {
c2fdda0d 2018 print "FUNC($1)\n" if ($dbg_values > 1);
c8cb2ca3 2019 $type = 'V';
cf655043 2020 $av_pending = 'V';
6c72ffaa 2021
8e761b04
AW
2022 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2023 if (defined $2 && $type eq 'C' || $type eq 'T') {
1f65f947 2024 $av_pend_colon = 'B';
8e761b04
AW
2025 } elsif ($type eq 'E') {
2026 $av_pend_colon = 'L';
1f65f947
AW
2027 }
2028 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2029 $type = 'V';
2030
6c72ffaa 2031 } elsif ($cur =~ /^($Ident|$Constant)/o) {
c2fdda0d 2032 print "IDENT($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
2033 $type = 'V';
2034
2035 } elsif ($cur =~ /^($Assignment)/o) {
c2fdda0d 2036 print "ASSIGN($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
2037 $type = 'N';
2038
cf655043 2039 } elsif ($cur =~/^(;|{|})/) {
c2fdda0d 2040 print "END($1)\n" if ($dbg_values > 1);
13214adf 2041 $type = 'E';
1f65f947
AW
2042 $av_pend_colon = 'O';
2043
8e761b04
AW
2044 } elsif ($cur =~/^(,)/) {
2045 print "COMMA($1)\n" if ($dbg_values > 1);
2046 $type = 'C';
2047
1f65f947
AW
2048 } elsif ($cur =~ /^(\?)/o) {
2049 print "QUESTION($1)\n" if ($dbg_values > 1);
2050 $type = 'N';
2051
2052 } elsif ($cur =~ /^(:)/o) {
2053 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2054
2055 substr($var, length($res), 1, $av_pend_colon);
2056 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2057 $type = 'E';
2058 } else {
2059 $type = 'N';
2060 }
2061 $av_pend_colon = 'O';
13214adf 2062
8e761b04 2063 } elsif ($cur =~ /^(\[)/o) {
13214adf 2064 print "CLOSE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
2065 $type = 'N';
2066
0d413866 2067 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
74048ed8
AW
2068 my $variant;
2069
2070 print "OPV($1)\n" if ($dbg_values > 1);
2071 if ($type eq 'V') {
2072 $variant = 'B';
2073 } else {
2074 $variant = 'U';
2075 }
2076
2077 substr($var, length($res), 1, $variant);
2078 $type = 'N';
2079
6c72ffaa 2080 } elsif ($cur =~ /^($Operators)/o) {
c2fdda0d 2081 print "OP($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
2082 if ($1 ne '++' && $1 ne '--') {
2083 $type = 'N';
2084 }
2085
2086 } elsif ($cur =~ /(^.)/o) {
c2fdda0d 2087 print "C($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
2088 }
2089 if (defined $1) {
2090 $cur = substr($cur, length($1));
2091 $res .= $type x length($1);
2092 }
9c0ca6f9 2093 }
0a920b5b 2094
1f65f947 2095 return ($res, $var);
0a920b5b
AW
2096}
2097
8905a67c 2098sub possible {
13214adf 2099 my ($possible, $line) = @_;
9a974fdb 2100 my $notPermitted = qr{(?:
0776e594
AW
2101 ^(?:
2102 $Modifier|
2103 $Storage|
2104 $Type|
9a974fdb
AW
2105 DEFINE_\S+
2106 )$|
2107 ^(?:
0776e594
AW
2108 goto|
2109 return|
2110 case|
2111 else|
2112 asm|__asm__|
89a88353
AW
2113 do|
2114 \#|
2115 \#\#|
9a974fdb 2116 )(?:\s|$)|
0776e594 2117 ^(?:typedef|struct|enum)\b
9a974fdb
AW
2118 )}x;
2119 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2120 if ($possible !~ $notPermitted) {
c45dcabd
AW
2121 # Check for modifiers.
2122 $possible =~ s/\s*$Storage\s*//g;
2123 $possible =~ s/\s*$Sparse\s*//g;
2124 if ($possible =~ /^\s*$/) {
2125
2126 } elsif ($possible =~ /\s/) {
2127 $possible =~ s/\s*$Type\s*//g;
d2506586 2128 for my $modifier (split(' ', $possible)) {
9a974fdb
AW
2129 if ($modifier !~ $notPermitted) {
2130 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
485ff23e 2131 push(@modifierListFile, $modifier);
9a974fdb 2132 }
d2506586 2133 }
c45dcabd
AW
2134
2135 } else {
2136 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
485ff23e 2137 push(@typeListFile, $possible);
c45dcabd 2138 }
8905a67c 2139 build_types();
0776e594
AW
2140 } else {
2141 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
8905a67c
AW
2142 }
2143}
2144
6c72ffaa
AW
2145my $prefix = '';
2146
000d1cc1 2147sub show_type {
cbec18af 2148 my ($type) = @_;
91bfe484 2149
522b837c
AD
2150 $type =~ tr/[a-z]/[A-Z]/;
2151
cbec18af
JP
2152 return defined $use_type{$type} if (scalar keys %use_type > 0);
2153
2154 return !defined $ignore_type{$type};
000d1cc1
JP
2155}
2156
f0a594c1 2157sub report {
cbec18af
JP
2158 my ($level, $type, $msg) = @_;
2159
2160 if (!show_type($type) ||
2161 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
773647a0
AW
2162 return 0;
2163 }
57230297 2164 my $output = '';
737c0767 2165 if ($color) {
57230297
JP
2166 if ($level eq 'ERROR') {
2167 $output .= RED;
2168 } elsif ($level eq 'WARNING') {
2169 $output .= YELLOW;
2170 } else {
2171 $output .= GREEN;
2172 }
2173 }
2174 $output .= $prefix . $level . ':';
000d1cc1 2175 if ($show_types) {
737c0767 2176 $output .= BLUE if ($color);
57230297 2177 $output .= "$type:";
000d1cc1 2178 }
737c0767 2179 $output .= RESET if ($color);
57230297 2180 $output .= ' ' . $msg . "\n";
34d8815f
JP
2181
2182 if ($showfile) {
2183 my @lines = split("\n", $output, -1);
2184 splice(@lines, 1, 1);
2185 $output = join("\n", @lines);
2186 }
57230297 2187 $output = (split('\n', $output))[0] . "\n" if ($terse);
8905a67c 2188
57230297 2189 push(our @report, $output);
773647a0
AW
2190
2191 return 1;
f0a594c1 2192}
cbec18af 2193
f0a594c1 2194sub report_dump {
13214adf 2195 our @report;
f0a594c1 2196}
000d1cc1 2197
d752fcc8
JP
2198sub fixup_current_range {
2199 my ($lineRef, $offset, $length) = @_;
2200
2201 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2202 my $o = $1;
2203 my $l = $2;
2204 my $no = $o + $offset;
2205 my $nl = $l + $length;
2206 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2207 }
2208}
2209
2210sub fix_inserted_deleted_lines {
2211 my ($linesRef, $insertedRef, $deletedRef) = @_;
2212
2213 my $range_last_linenr = 0;
2214 my $delta_offset = 0;
2215
2216 my $old_linenr = 0;
2217 my $new_linenr = 0;
2218
2219 my $next_insert = 0;
2220 my $next_delete = 0;
2221
2222 my @lines = ();
2223
2224 my $inserted = @{$insertedRef}[$next_insert++];
2225 my $deleted = @{$deletedRef}[$next_delete++];
2226
2227 foreach my $old_line (@{$linesRef}) {
2228 my $save_line = 1;
2229 my $line = $old_line; #don't modify the array
323b267f 2230 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
d752fcc8
JP
2231 $delta_offset = 0;
2232 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2233 $range_last_linenr = $new_linenr;
2234 fixup_current_range(\$line, $delta_offset, 0);
2235 }
2236
2237 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2238 $deleted = @{$deletedRef}[$next_delete++];
2239 $save_line = 0;
2240 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2241 }
2242
2243 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2244 push(@lines, ${$inserted}{'LINE'});
2245 $inserted = @{$insertedRef}[$next_insert++];
2246 $new_linenr++;
2247 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2248 }
2249
2250 if ($save_line) {
2251 push(@lines, $line);
2252 $new_linenr++;
2253 }
2254
2255 $old_linenr++;
2256 }
2257
2258 return @lines;
2259}
2260
f2d7e4d4
JP
2261sub fix_insert_line {
2262 my ($linenr, $line) = @_;
2263
2264 my $inserted = {
2265 LINENR => $linenr,
2266 LINE => $line,
2267 };
2268 push(@fixed_inserted, $inserted);
2269}
2270
2271sub fix_delete_line {
2272 my ($linenr, $line) = @_;
2273
2274 my $deleted = {
2275 LINENR => $linenr,
2276 LINE => $line,
2277 };
2278
2279 push(@fixed_deleted, $deleted);
2280}
2281
de7d4f0e 2282sub ERROR {
cbec18af
JP
2283 my ($type, $msg) = @_;
2284
2285 if (report("ERROR", $type, $msg)) {
773647a0
AW
2286 our $clean = 0;
2287 our $cnt_error++;
3705ce5b 2288 return 1;
773647a0 2289 }
3705ce5b 2290 return 0;
de7d4f0e
AW
2291}
2292sub WARN {
cbec18af
JP
2293 my ($type, $msg) = @_;
2294
2295 if (report("WARNING", $type, $msg)) {
773647a0
AW
2296 our $clean = 0;
2297 our $cnt_warn++;
3705ce5b 2298 return 1;
773647a0 2299 }
3705ce5b 2300 return 0;
de7d4f0e
AW
2301}
2302sub CHK {
cbec18af
JP
2303 my ($type, $msg) = @_;
2304
2305 if ($check && report("CHECK", $type, $msg)) {
6c72ffaa
AW
2306 our $clean = 0;
2307 our $cnt_chk++;
3705ce5b 2308 return 1;
6c72ffaa 2309 }
3705ce5b 2310 return 0;
de7d4f0e
AW
2311}
2312
6ecd9674
AW
2313sub check_absolute_file {
2314 my ($absolute, $herecurr) = @_;
2315 my $file = $absolute;
2316
2317 ##print "absolute<$absolute>\n";
2318
2319 # See if any suffix of this path is a path within the tree.
2320 while ($file =~ s@^[^/]*/@@) {
2321 if (-f "$root/$file") {
2322 ##print "file<$file>\n";
2323 last;
2324 }
2325 }
2326 if (! -f _) {
2327 return 0;
2328 }
2329
2330 # It is, so see if the prefix is acceptable.
2331 my $prefix = $absolute;
2332 substr($prefix, -length($file)) = '';
2333
2334 ##print "prefix<$prefix>\n";
2335 if ($prefix ne ".../") {
000d1cc1
JP
2336 WARN("USE_RELATIVE_PATH",
2337 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
6ecd9674
AW
2338 }
2339}
2340
3705ce5b
JP
2341sub trim {
2342 my ($string) = @_;
2343
b34c648b
JP
2344 $string =~ s/^\s+|\s+$//g;
2345
2346 return $string;
2347}
2348
2349sub ltrim {
2350 my ($string) = @_;
2351
2352 $string =~ s/^\s+//;
2353
2354 return $string;
2355}
2356
2357sub rtrim {
2358 my ($string) = @_;
2359
2360 $string =~ s/\s+$//;
3705ce5b
JP
2361
2362 return $string;
2363}
2364
52ea8506
JP
2365sub string_find_replace {
2366 my ($string, $find, $replace) = @_;
2367
2368 $string =~ s/$find/$replace/g;
2369
2370 return $string;
2371}
2372
3705ce5b
JP
2373sub tabify {
2374 my ($leading) = @_;
2375
713a09de 2376 my $source_indent = $tabsize;
3705ce5b
JP
2377 my $max_spaces_before_tab = $source_indent - 1;
2378 my $spaces_to_tab = " " x $source_indent;
2379
2380 #convert leading spaces to tabs
2381 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2382 #Remove spaces before a tab
2383 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2384
2385 return "$leading";
2386}
2387
d1fe9c09
JP
2388sub pos_last_openparen {
2389 my ($line) = @_;
2390
2391 my $pos = 0;
2392
2393 my $opens = $line =~ tr/\(/\(/;
2394 my $closes = $line =~ tr/\)/\)/;
2395
2396 my $last_openparen = 0;
2397
2398 if (($opens == 0) || ($closes >= $opens)) {
2399 return -1;
2400 }
2401
2402 my $len = length($line);
2403
2404 for ($pos = 0; $pos < $len; $pos++) {
2405 my $string = substr($line, $pos);
2406 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2407 $pos += length($1) - 1;
2408 } elsif (substr($line, $pos, 1) eq '(') {
2409 $last_openparen = $pos;
2410 } elsif (index($string, '(') == -1) {
2411 last;
2412 }
2413 }
2414
91cb5195 2415 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
d1fe9c09
JP
2416}
2417
f36d3eb8
JP
2418sub get_raw_comment {
2419 my ($line, $rawline) = @_;
2420 my $comment = '';
2421
2422 for my $i (0 .. (length($line) - 1)) {
2423 if (substr($line, $i, 1) eq "$;") {
2424 $comment .= substr($rawline, $i, 1);
2425 }
2426 }
2427
2428 return $comment;
2429}
2430
0a920b5b
AW
2431sub process {
2432 my $filename = shift;
0a920b5b
AW
2433
2434 my $linenr=0;
2435 my $prevline="";
c2fdda0d 2436 my $prevrawline="";
0a920b5b 2437 my $stashline="";
c2fdda0d 2438 my $stashrawline="";
0a920b5b 2439
4a0df2ef 2440 my $length;
0a920b5b
AW
2441 my $indent;
2442 my $previndent=0;
2443 my $stashindent=0;
2444
de7d4f0e 2445 our $clean = 1;
0a920b5b 2446 my $signoff = 0;
cd261496
GU
2447 my $author = '';
2448 my $authorsignoff = 0;
48ca2d8a 2449 my $author_sob = '';
0a920b5b 2450 my $is_patch = 0;
133712a2 2451 my $is_binding_patch = -1;
29ee1b0c 2452 my $in_header_lines = $file ? 0 : 1;
15662b3e 2453 my $in_commit_log = 0; #Scanning lines before patch
44d303eb 2454 my $has_patch_separator = 0; #Found a --- line
ed43c4e5 2455 my $has_commit_log = 0; #Encountered lines before patch
490b292c 2456 my $commit_log_lines = 0; #Number of commit log lines
77cb8546 2457 my $commit_log_possible_stack_dump = 0;
2a076f40 2458 my $commit_log_long_line = 0;
e518e9a5 2459 my $commit_log_has_diff = 0;
13f1937e 2460 my $reported_maintainer_file = 0;
fa64205d
PS
2461 my $non_utf8_charset = 0;
2462
365dd4ea 2463 my $last_blank_line = 0;
5e4f6ba5 2464 my $last_coalesced_string_linenr = -1;
365dd4ea 2465
13214adf 2466 our @report = ();
6c72ffaa
AW
2467 our $cnt_lines = 0;
2468 our $cnt_error = 0;
2469 our $cnt_warn = 0;
2470 our $cnt_chk = 0;
2471
0a920b5b
AW
2472 # Trace the real file/line as we go.
2473 my $realfile = '';
2474 my $realline = 0;
2475 my $realcnt = 0;
2476 my $here = '';
77cb8546 2477 my $context_function; #undef'd unless there's a known function
0a920b5b 2478 my $in_comment = 0;
c2fdda0d 2479 my $comment_edge = 0;
0a920b5b 2480 my $first_line = 0;
1e855726 2481 my $p1_prefix = '';
0a920b5b 2482
13214adf
AW
2483 my $prev_values = 'E';
2484
2485 # suppression flags
773647a0 2486 my %suppress_ifbraces;
170d3a22 2487 my %suppress_whiletrailers;
2b474a1a 2488 my %suppress_export;
3e469cdc 2489 my $suppress_statement = 0;
653d4876 2490
7e51f197 2491 my %signatures = ();
323c1260 2492
c2fdda0d 2493 # Pre-scan the patch sanitizing the lines.
de7d4f0e 2494 # Pre-scan the patch looking for any __setup documentation.
c2fdda0d 2495 #
de7d4f0e
AW
2496 my @setup_docs = ();
2497 my $setup_docs = 0;
773647a0 2498
d8b07710
JP
2499 my $camelcase_file_seeded = 0;
2500
9f3a8992
RH
2501 my $checklicenseline = 1;
2502
773647a0 2503 sanitise_line_reset();
c2fdda0d
AW
2504 my $line;
2505 foreach my $rawline (@rawlines) {
773647a0
AW
2506 $linenr++;
2507 $line = $rawline;
c2fdda0d 2508
3705ce5b
JP
2509 push(@fixed, $rawline) if ($fix);
2510
773647a0 2511 if ($rawline=~/^\+\+\+\s+(\S+)/) {
de7d4f0e 2512 $setup_docs = 0;
2581ac7c 2513 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
de7d4f0e
AW
2514 $setup_docs = 1;
2515 }
773647a0
AW
2516 #next;
2517 }
74fd4f34 2518 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
773647a0
AW
2519 $realline=$1-1;
2520 if (defined $2) {
2521 $realcnt=$3+1;
2522 } else {
2523 $realcnt=1+1;
2524 }
c45dcabd 2525 $in_comment = 0;
773647a0
AW
2526
2527 # Guestimate if this is a continuing comment. Run
2528 # the context looking for a comment "edge". If this
2529 # edge is a close comment then we must be in a comment
2530 # at context start.
2531 my $edge;
01fa9147
AW
2532 my $cnt = $realcnt;
2533 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2534 next if (defined $rawlines[$ln - 1] &&
2535 $rawlines[$ln - 1] =~ /^-/);
2536 $cnt--;
2537 #print "RAW<$rawlines[$ln - 1]>\n";
721c1cb6 2538 last if (!defined $rawlines[$ln - 1]);
fae17dae
AW
2539 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2540 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2541 ($edge) = $1;
2542 last;
2543 }
773647a0
AW
2544 }
2545 if (defined $edge && $edge eq '*/') {
2546 $in_comment = 1;
2547 }
2548
2549 # Guestimate if this is a continuing comment. If this
2550 # is the start of a diff block and this line starts
2551 # ' *' then it is very likely a comment.
2552 if (!defined $edge &&
83242e0c 2553 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
773647a0
AW
2554 {
2555 $in_comment = 1;
2556 }
2557
2558 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2559 sanitise_line_reset($in_comment);
2560
171ae1a4 2561 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
773647a0 2562 # Standardise the strings and chars within the input to
171ae1a4 2563 # simplify matching -- only bother with positive lines.
773647a0 2564 $line = sanitise_line($rawline);
de7d4f0e 2565 }
773647a0
AW
2566 push(@lines, $line);
2567
2568 if ($realcnt > 1) {
2569 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2570 } else {
2571 $realcnt = 0;
2572 }
2573
2574 #print "==>$rawline\n";
2575 #print "-->$line\n";
de7d4f0e
AW
2576
2577 if ($setup_docs && $line =~ /^\+/) {
2578 push(@setup_docs, $line);
2579 }
2580 }
2581
6c72ffaa
AW
2582 $prefix = '';
2583
773647a0
AW
2584 $realcnt = 0;
2585 $linenr = 0;
194f66fc 2586 $fixlinenr = -1;
0a920b5b
AW
2587 foreach my $line (@lines) {
2588 $linenr++;
194f66fc 2589 $fixlinenr++;
1b5539b1
JP
2590 my $sline = $line; #copy of $line
2591 $sline =~ s/$;/ /g; #with comments as spaces
0a920b5b 2592
c2fdda0d 2593 my $rawline = $rawlines[$linenr - 1];
f36d3eb8 2594 my $raw_comment = get_raw_comment($line, $rawline);
6c72ffaa 2595
12c253ab
JP
2596# check if it's a mode change, rename or start of a patch
2597 if (!$in_commit_log &&
2598 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2599 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2600 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2601 $is_patch = 1;
2602 }
2603
0a920b5b 2604#extract the line range in the file after the patch is applied
e518e9a5 2605 if (!$in_commit_log &&
74fd4f34
JP
2606 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2607 my $context = $4;
0a920b5b 2608 $is_patch = 1;
4a0df2ef 2609 $first_line = $linenr + 1;
0a920b5b
AW
2610 $realline=$1-1;
2611 if (defined $2) {
2612 $realcnt=$3+1;
2613 } else {
2614 $realcnt=1+1;
2615 }
c2fdda0d 2616 annotate_reset();
13214adf
AW
2617 $prev_values = 'E';
2618
773647a0 2619 %suppress_ifbraces = ();
170d3a22 2620 %suppress_whiletrailers = ();
2b474a1a 2621 %suppress_export = ();
3e469cdc 2622 $suppress_statement = 0;
74fd4f34
JP
2623 if ($context =~ /\b(\w+)\s*\(/) {
2624 $context_function = $1;
2625 } else {
2626 undef $context_function;
2627 }
0a920b5b 2628 next;
0a920b5b 2629
4a0df2ef
AW
2630# track the line number as we move through the hunk, note that
2631# new versions of GNU diff omit the leading space on completely
2632# blank context lines so we need to count that too.
773647a0 2633 } elsif ($line =~ /^( |\+|$)/) {
0a920b5b 2634 $realline++;
d8aaf121 2635 $realcnt-- if ($realcnt != 0);
0a920b5b 2636
4a0df2ef 2637 # Measure the line length and indent.
c2fdda0d 2638 ($length, $indent) = line_stats($rawline);
0a920b5b
AW
2639
2640 # Track the previous line.
2641 ($prevline, $stashline) = ($stashline, $line);
2642 ($previndent, $stashindent) = ($stashindent, $indent);
c2fdda0d
AW
2643 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2644
773647a0 2645 #warn "line<$line>\n";
6c72ffaa 2646
d8aaf121
AW
2647 } elsif ($realcnt == 1) {
2648 $realcnt--;
0a920b5b
AW
2649 }
2650
cc77cdca
AW
2651 my $hunk_line = ($realcnt != 0);
2652
6c72ffaa
AW
2653 $here = "#$linenr: " if (!$file);
2654 $here = "#$realline: " if ($file);
773647a0 2655
2ac73b4f 2656 my $found_file = 0;
773647a0 2657 # extract the filename as it passes
3bf9a009
RV
2658 if ($line =~ /^diff --git.*?(\S+)$/) {
2659 $realfile = $1;
2b7ab453 2660 $realfile =~ s@^([^/]*)/@@ if (!$file);
270c49a0 2661 $in_commit_log = 0;
2ac73b4f 2662 $found_file = 1;
3bf9a009 2663 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
773647a0 2664 $realfile = $1;
2b7ab453 2665 $realfile =~ s@^([^/]*)/@@ if (!$file);
270c49a0 2666 $in_commit_log = 0;
1e855726
WS
2667
2668 $p1_prefix = $1;
e2f7aa4b
AW
2669 if (!$file && $tree && $p1_prefix ne '' &&
2670 -e "$root/$p1_prefix") {
000d1cc1
JP
2671 WARN("PATCH_PREFIX",
2672 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1e855726 2673 }
773647a0 2674
c1ab3326 2675 if ($realfile =~ m@^include/asm/@) {
000d1cc1
JP
2676 ERROR("MODIFIED_INCLUDE_ASM",
2677 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
773647a0 2678 }
2ac73b4f
JP
2679 $found_file = 1;
2680 }
2681
34d8815f
JP
2682#make up the handle for any error we report on this line
2683 if ($showfile) {
2684 $prefix = "$realfile:$realline: "
2685 } elsif ($emacs) {
7d3a9f67
JP
2686 if ($file) {
2687 $prefix = "$filename:$realline: ";
2688 } else {
2689 $prefix = "$filename:$linenr: ";
2690 }
34d8815f
JP
2691 }
2692
2ac73b4f 2693 if ($found_file) {
85b0ee18
JP
2694 if (is_maintained_obsolete($realfile)) {
2695 WARN("OBSOLETE",
2696 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2697 }
7bd7e483 2698 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2ac73b4f
JP
2699 $check = 1;
2700 } else {
2701 $check = $check_orig;
2702 }
9f3a8992 2703 $checklicenseline = 1;
133712a2
RH
2704
2705 if ($realfile !~ /^MAINTAINERS/) {
2706 my $last_binding_patch = $is_binding_patch;
2707
2708 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2709
2710 if (($last_binding_patch != -1) &&
2711 ($last_binding_patch ^ $is_binding_patch)) {
2712 WARN("DT_SPLIT_BINDING_PATCH",
858e6845 2713 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
133712a2
RH
2714 }
2715 }
2716
773647a0
AW
2717 next;
2718 }
2719
389834b6 2720 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
0a920b5b 2721
c2fdda0d
AW
2722 my $hereline = "$here\n$rawline\n";
2723 my $herecurr = "$here\n$rawline\n";
2724 my $hereprev = "$here\n$prevrawline\n$rawline\n";
0a920b5b 2725
6c72ffaa
AW
2726 $cnt_lines++ if ($realcnt != 0);
2727
490b292c
JP
2728# Verify the existence of a commit log if appropriate
2729# 2 is used because a $signature is counted in $commit_log_lines
2730 if ($in_commit_log) {
2731 if ($line !~ /^\s*$/) {
2732 $commit_log_lines++; #could be a $signature
2733 }
2734 } elsif ($has_commit_log && $commit_log_lines < 2) {
2735 WARN("COMMIT_MESSAGE",
2736 "Missing commit description - Add an appropriate one\n");
2737 $commit_log_lines = 2; #warn only once
2738 }
2739
e518e9a5
JP
2740# Check if the commit log has what seems like a diff which can confuse patch
2741 if ($in_commit_log && !$commit_log_has_diff &&
13e45417
MP
2742 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2743 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
e518e9a5
JP
2744 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2745 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2746 ERROR("DIFF_IN_COMMIT_MSG",
2747 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2748 $commit_log_has_diff = 1;
2749 }
2750
3bf9a009
RV
2751# Check for incorrect file permissions
2752 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2753 my $permhere = $here . "FILE: $realfile\n";
04db4d25
JP
2754 if ($realfile !~ m@scripts/@ &&
2755 $realfile !~ /\.(py|pl|awk|sh)$/) {
000d1cc1
JP
2756 ERROR("EXECUTE_PERMISSIONS",
2757 "do not set execute permissions for source files\n" . $permhere);
3bf9a009
RV
2758 }
2759 }
2760
cd261496
GU
2761# Check the patch for a From:
2762 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2763 $author = $1;
e7f929f3
DR
2764 my $curline = $linenr;
2765 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2766 $author .= $1;
2767 }
cd261496
GU
2768 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2769 $author =~ s/"//g;
dfa05c28 2770 $author = reformat_email($author);
cd261496
GU
2771 }
2772
20112475 2773# Check the patch for a signoff:
dfa05c28 2774 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
4a0df2ef 2775 $signoff++;
15662b3e 2776 $in_commit_log = 0;
48ca2d8a 2777 if ($author ne '' && $authorsignoff != 1) {
fccaebf0 2778 if (same_email_addresses($1, $author)) {
dfa05c28 2779 $authorsignoff = 1;
48ca2d8a
DR
2780 } else {
2781 my $ctx = $1;
2782 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2783 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2784
2785 if ($email_address eq $author_address && $email_name eq $author_name) {
2786 $author_sob = $ctx;
2787 $authorsignoff = 2;
2788 } elsif ($email_address eq $author_address) {
2789 $author_sob = $ctx;
2790 $authorsignoff = 3;
2791 } elsif ($email_name eq $author_name) {
2792 $author_sob = $ctx;
2793 $authorsignoff = 4;
2794
2795 my $address1 = $email_address;
2796 my $address2 = $author_address;
2797
2798 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2799 $address1 = "$1$2";
2800 }
2801 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2802 $address2 = "$1$2";
2803 }
2804 if ($address1 eq $address2) {
2805 $authorsignoff = 5;
2806 }
2807 }
cd261496
GU
2808 }
2809 }
20112475
JP
2810 }
2811
44d303eb
JP
2812# Check for patch separator
2813 if ($line =~ /^---$/) {
2814 $has_patch_separator = 1;
2815 $in_commit_log = 0;
2816 }
2817
e0d975b1
JP
2818# Check if MAINTAINERS is being updated. If so, there's probably no need to
2819# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2820 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2821 $reported_maintainer_file = 1;
2822 }
2823
20112475 2824# Check signature styles
270c49a0 2825 if (!$in_header_lines &&
ce0338df 2826 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
20112475
JP
2827 my $space_before = $1;
2828 my $sign_off = $2;
2829 my $space_after = $3;
2830 my $email = $4;
2831 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2832
ce0338df 2833 if ($sign_off !~ /$signature_tags/) {
831242ab
AS
2834 my $suggested_signature = find_standard_signature($sign_off);
2835 if ($suggested_signature eq "") {
2836 WARN("BAD_SIGN_OFF",
2837 "Non-standard signature: $sign_off\n" . $herecurr);
2838 } else {
2839 if (WARN("BAD_SIGN_OFF",
2840 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2841 $fix) {
2842 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2843 }
2844 }
ce0338df 2845 }
20112475 2846 if (defined $space_before && $space_before ne "") {
3705ce5b
JP
2847 if (WARN("BAD_SIGN_OFF",
2848 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2849 $fix) {
194f66fc 2850 $fixed[$fixlinenr] =
3705ce5b
JP
2851 "$ucfirst_sign_off $email";
2852 }
20112475
JP
2853 }
2854 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
3705ce5b
JP
2855 if (WARN("BAD_SIGN_OFF",
2856 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2857 $fix) {
194f66fc 2858 $fixed[$fixlinenr] =
3705ce5b
JP
2859 "$ucfirst_sign_off $email";
2860 }
2861
20112475
JP
2862 }
2863 if (!defined $space_after || $space_after ne " ") {
3705ce5b
JP
2864 if (WARN("BAD_SIGN_OFF",
2865 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2866 $fix) {
194f66fc 2867 $fixed[$fixlinenr] =
3705ce5b
JP
2868 "$ucfirst_sign_off $email";
2869 }
0a920b5b 2870 }
20112475 2871
dfa05c28 2872 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
48ca2d8a 2873 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
20112475 2874 if ($suggested_email eq "") {
000d1cc1
JP
2875 ERROR("BAD_SIGN_OFF",
2876 "Unrecognized email address: '$email'\n" . $herecurr);
20112475
JP
2877 } else {
2878 my $dequoted = $suggested_email;
2879 $dequoted =~ s/^"//;
2880 $dequoted =~ s/" </ </;
2881 # Don't force email to have quotes
2882 # Allow just an angle bracketed address
fccaebf0
DR
2883 if (!same_email_addresses($email, $suggested_email)) {
2884 if (WARN("BAD_SIGN_OFF",
2885 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2886 $fix) {
2887 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2888 }
2889 }
2890
2891 # Address part shouldn't have comments
2892 my $stripped_address = $email_address;
2893 $stripped_address =~ s/\([^\(\)]*\)//g;
2894 if ($email_address ne $stripped_address) {
2895 if (WARN("BAD_SIGN_OFF",
2896 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2897 $fix) {
2898 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2899 }
2900 }
2901
2902 # Only one name comment should be allowed
2903 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2904 if ($comment_count > 1) {
000d1cc1 2905 WARN("BAD_SIGN_OFF",
fccaebf0
DR
2906 "Use a single name comment in email: '$email'\n" . $herecurr);
2907 }
2908
2909
2910 # stable@vger.kernel.org or stable@kernel.org shouldn't
e73d2715 2911 # have an email name. In addition comments should strictly
fccaebf0
DR
2912 # begin with a #
2913 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2914 if (($comment ne "" && $comment !~ /^#.+/) ||
2915 ($email_name ne "")) {
2916 my $cur_name = $email_name;
2917 my $new_comment = $comment;
2918 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2919
2920 # Remove brackets enclosing comment text
2921 # and # from start of comments to get comment text
2922 $new_comment =~ s/^\((.*)\)$/$1/;
2923 $new_comment =~ s/^\[(.*)\]$/$1/;
2924 $new_comment =~ s/^[\s\#]+|\s+$//g;
2925
2926 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2927 $new_comment = " # $new_comment" if ($new_comment ne "");
2928 my $new_email = "$email_address$new_comment";
2929
2930 if (WARN("BAD_STABLE_ADDRESS_STYLE",
2931 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2932 $fix) {
2933 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2934 }
2935 }
2936 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2937 my $new_comment = $comment;
2938
2939 # Extract comment text from within brackets or
2940 # c89 style /*...*/ comments
2941 $new_comment =~ s/^\[(.*)\]$/$1/;
2942 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2943
2944 $new_comment = trim($new_comment);
2945 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2946 $new_comment = "($new_comment)" if ($new_comment ne "");
2947 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2948
2949 if (WARN("BAD_SIGN_OFF",
2950 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2951 $fix) {
2952 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2953 }
20112475 2954 }
0a920b5b 2955 }
7e51f197
JP
2956
2957# Check for duplicate signatures
2958 my $sig_nospace = $line;
2959 $sig_nospace =~ s/\s//g;
2960 $sig_nospace = lc($sig_nospace);
2961 if (defined $signatures{$sig_nospace}) {
2962 WARN("BAD_SIGN_OFF",
2963 "Duplicate signature\n" . $herecurr);
2964 } else {
2965 $signatures{$sig_nospace} = 1;
2966 }
6c5d24ee
SC
2967
2968# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2969 if ($sign_off =~ /^co-developed-by:$/i) {
2970 if ($email eq $author) {
2971 WARN("BAD_SIGN_OFF",
2972 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2973 }
2974 if (!defined $lines[$linenr]) {
2975 WARN("BAD_SIGN_OFF",
2976 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2977 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2978 WARN("BAD_SIGN_OFF",
2979 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2980 } elsif ($1 ne $email) {
2981 WARN("BAD_SIGN_OFF",
2982 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2983 }
2984 }
0a920b5b
AW
2985 }
2986
a2fe16b9
JP
2987# Check email subject for common tools that don't need to be mentioned
2988 if ($in_header_lines &&
2989 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2990 WARN("EMAIL_SUBJECT",
2991 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2992 }
2993
44d303eb
JP
2994# Check for Gerrit Change-Ids not in any patch context
2995 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
7580c5b9
AS
2996 if (ERROR("GERRIT_CHANGE_ID",
2997 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
2998 $fix) {
2999 fix_delete_line($fixlinenr, $rawline);
3000 }
7ebd05ef
CC
3001 }
3002
369c8dd3
JP
3003# Check if the commit log is in a possible stack dump
3004 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3005 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3006 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3007 # timestamp
634cffcc
JP
3008 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3009 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3010 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3011 # stack dump address styles
369c8dd3
JP
3012 $commit_log_possible_stack_dump = 1;
3013 }
3014
2a076f40
JP
3015# Check for line lengths > 75 in commit log, warn once
3016 if ($in_commit_log && !$commit_log_long_line &&
369c8dd3
JP
3017 length($line) > 75 &&
3018 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3019 # file delta changes
3020 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3021 # filename then :
27b379af
AS
3022 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3023 # A Fixes: or Link: line or signature tag line
369c8dd3 3024 $commit_log_possible_stack_dump)) {
2a076f40
JP
3025 WARN("COMMIT_LOG_LONG_LINE",
3026 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3027 $commit_log_long_line = 1;
3028 }
3029
bf4daf12 3030# Reset possible stack dump if a blank line is found
369c8dd3
JP
3031 if ($in_commit_log && $commit_log_possible_stack_dump &&
3032 $line =~ /^\s*$/) {
3033 $commit_log_possible_stack_dump = 0;
3034 }
bf4daf12 3035
084a617a
DR
3036# Check for lines starting with a #
3037 if ($in_commit_log && $line =~ /^#/) {
3038 if (WARN("COMMIT_COMMENT_SYMBOL",
3039 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3040 $fix) {
3041 $fixed[$fixlinenr] =~ s/^/ /;
3042 }
3043 }
3044
0d7835fc 3045# Check for git id commit length and improperly formed commit descriptions
369c8dd3 3046 if ($in_commit_log && !$commit_log_possible_stack_dump &&
a8972573 3047 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
e882dbfc 3048 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
fe043ea1 3049 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
aab38f51 3050 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
369c8dd3
JP
3051 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3052 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
fe043ea1
JP
3053 my $init_char = "c";
3054 my $orig_commit = "";
0d7835fc
JP
3055 my $short = 1;
3056 my $long = 0;
3057 my $case = 1;
3058 my $space = 1;
3059 my $hasdesc = 0;
19c146a6 3060 my $hasparens = 0;
0d7835fc
JP
3061 my $id = '0123456789ab';
3062 my $orig_desc = "commit description";
3063 my $description = "";
3064
fe043ea1
JP
3065 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3066 $init_char = $1;
3067 $orig_commit = lc($2);
3068 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3069 $orig_commit = lc($1);
3070 }
3071
0d7835fc
JP
3072 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3073 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
3074 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
3075 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3076 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
3077 $orig_desc = $1;
19c146a6 3078 $hasparens = 1;
0d7835fc
JP
3079 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
3080 defined $rawlines[$linenr] &&
3081 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
3082 $orig_desc = $1;
19c146a6 3083 $hasparens = 1;
b671fde0
JP
3084 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3085 defined $rawlines[$linenr] &&
3086 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3087 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3088 $orig_desc = $1;
3089 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3090 $orig_desc .= " " . $1;
19c146a6 3091 $hasparens = 1;
0d7835fc
JP
3092 }
3093
3094 ($id, $description) = git_commit_info($orig_commit,
3095 $id, $orig_desc);
3096
948b133a
HS
3097 if (defined($id) &&
3098 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
0d7835fc
JP
3099 ERROR("GIT_COMMIT_ID",
3100 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
3101 }
d311cd44
JP
3102 }
3103
13f1937e
JP
3104# Check for added, moved or deleted files
3105 if (!$reported_maintainer_file && !$in_commit_log &&
3106 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3107 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3108 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3109 (defined($1) || defined($2))))) {
a82603a8 3110 $is_patch = 1;
13f1937e
JP
3111 $reported_maintainer_file = 1;
3112 WARN("FILE_PATH_CHANGES",
3113 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3114 }
3115
e400edb1
RH
3116# Check for adding new DT bindings not in schema format
3117 if (!$in_commit_log &&
3118 ($line =~ /^new file mode\s*\d+\s*$/) &&
3119 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3120 WARN("DT_SCHEMA_BINDING_PATCH",
3121 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3122 }
3123
00df344f 3124# Check for wrappage within a valid hunk of the file
8905a67c 3125 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
000d1cc1
JP
3126 ERROR("CORRUPTED_PATCH",
3127 "patch seems to be corrupt (line wrapped?)\n" .
6c72ffaa 3128 $herecurr) if (!$emitted_corrupt++);
de7d4f0e
AW
3129 }
3130
3131# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3132 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
171ae1a4
AW
3133 $rawline !~ m/^$UTF8*$/) {
3134 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3135
3136 my $blank = copy_spacing($rawline);
3137 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3138 my $hereptr = "$hereline$ptr\n";
3139
34d99219
JP
3140 CHK("INVALID_UTF8",
3141 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
00df344f
AW
3142 }
3143
15662b3e
JP
3144# Check if it's the start of a commit log
3145# (not a header line and we haven't seen the patch filename)
3146 if ($in_header_lines && $realfile =~ /^$/ &&
eb3a58de
JP
3147 !($rawline =~ /^\s+(?:\S|$)/ ||
3148 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
15662b3e
JP
3149 $in_header_lines = 0;
3150 $in_commit_log = 1;
ed43c4e5 3151 $has_commit_log = 1;
15662b3e
JP
3152 }
3153
fa64205d
PS
3154# Check if there is UTF-8 in a commit log when a mail header has explicitly
3155# declined it, i.e defined some charset where it is missing.
3156 if ($in_header_lines &&
3157 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3158 $1 !~ /utf-8/i) {
3159 $non_utf8_charset = 1;
3160 }
3161
3162 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
15662b3e 3163 $rawline =~ /$NON_ASCII_UTF8/) {
fa64205d 3164 WARN("UTF8_BEFORE_PATCH",
15662b3e
JP
3165 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3166 }
3167
d6430f71
JP
3168# Check for absolute kernel paths in commit message
3169 if ($tree && $in_commit_log) {
3170 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3171 my $file = $1;
3172
3173 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3174 check_absolute_file($1, $herecurr)) {
3175 #
3176 } else {
3177 check_absolute_file($file, $herecurr);
3178 }
3179 }
3180 }
3181
66b47b4a 3182# Check for various typo / spelling mistakes
66d7a382
JP
3183 if (defined($misspellings) &&
3184 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
7da07c31 3185 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
66b47b4a 3186 my $typo = $1;
7da07c31
DR
3187 my $blank = copy_spacing($rawline);
3188 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3189 my $hereptr = "$hereline$ptr\n";
66b47b4a
KC
3190 my $typo_fix = $spelling_fix{lc($typo)};
3191 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3192 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
0675a8fb
JD
3193 my $msg_level = \&WARN;
3194 $msg_level = \&CHK if ($file);
3195 if (&{$msg_level}("TYPO_SPELLING",
7da07c31 3196 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
66b47b4a
KC
3197 $fix) {
3198 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3199 }
3200 }
3201 }
3202
a8dd86bf
MC
3203# check for invalid commit id
3204 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3205 my $id;
3206 my $description;
3207 ($id, $description) = git_commit_info($2, undef, undef);
3208 if (!defined($id)) {
3209 WARN("UNKNOWN_COMMIT_ID",
3210 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3211 }
3212 }
3213
310cd06b 3214# check for repeated words separated by a single space
8d0325cc
AS
3215# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3216 if (($rawline =~ /^\+/ || $in_commit_log) &&
3217 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
1db81a68 3218 pos($rawline) = 1 if (!$in_commit_log);
310cd06b
JP
3219 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3220
3221 my $first = $1;
3222 my $second = $2;
1db81a68
DR
3223 my $start_pos = $-[1];
3224 my $end_pos = $+[2];
310cd06b
JP
3225 if ($first =~ /(?:struct|union|enum)/) {
3226 pos($rawline) += length($first) + length($second) + 1;
3227 next;
3228 }
3229
1db81a68 3230 next if (lc($first) ne lc($second));
310cd06b
JP
3231 next if ($first eq 'long');
3232
1db81a68
DR
3233 # check for character before and after the word matches
3234 my $start_char = '';
3235 my $end_char = '';
3236 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3237 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3238
3239 next if ($start_char =~ /^\S$/);
3240 next if (index(" \t.,;?!", $end_char) == -1);
3241
8d0325cc
AS
3242 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3243 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3244 next if (!exists($allow_repeated_words{lc($first)}));
3245 }
3246
310cd06b
JP
3247 if (WARN("REPEATED_WORD",
3248 "Possible repeated word: '$first'\n" . $herecurr) &&
3249 $fix) {
3250 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3251 }
3252 }
3253
3254 # if it's a repeated word on consecutive lines in a comment block
3255 if ($prevline =~ /$;+\s*$/ &&
3256 $prevrawline =~ /($word_pattern)\s*$/) {
3257 my $last_word = $1;
3258 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3259 if (WARN("REPEATED_WORD",
3260 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3261 $fix) {
3262 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3263 }
3264 }
3265 }
3266 }
3267
30670854
AW
3268# ignore non-hunk lines and lines being removed
3269 next if (!$hunk_line || $line =~ /^-/);
0a920b5b 3270
0a920b5b 3271#trailing whitespace
9c0ca6f9 3272 if ($line =~ /^\+.*\015/) {
c2fdda0d 3273 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
d5e616fc
JP
3274 if (ERROR("DOS_LINE_ENDINGS",
3275 "DOS line endings\n" . $herevet) &&
3276 $fix) {
194f66fc 3277 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
d5e616fc 3278 }
c2fdda0d
AW
3279 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3280 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3705ce5b
JP
3281 if (ERROR("TRAILING_WHITESPACE",
3282 "trailing whitespace\n" . $herevet) &&
3283 $fix) {
194f66fc 3284 $fixed[$fixlinenr] =~ s/\s+$//;
3705ce5b
JP
3285 }
3286
d2c0a235 3287 $rpt_cleaners = 1;
0a920b5b 3288 }
5368df20 3289
4783f894 3290# Check for FSF mailing addresses.
109d8cb2 3291 if ($rawline =~ /\bwrite to the Free/i ||
1bde561e 3292 $rawline =~ /\b675\s+Mass\s+Ave/i ||
3e2232f2
JP
3293 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3294 $rawline =~ /\b51\s+Franklin\s+St/i) {
4783f894 3295 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
0675a8fb
JD
3296 my $msg_level = \&ERROR;
3297 $msg_level = \&CHK if ($file);
3298 &{$msg_level}("FSF_MAILING_ADDRESS",
3299 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
4783f894
JT
3300 }
3301
3354957a 3302# check for Kconfig help text having a real description
9fe287d7
AW
3303# Only applies when adding the entry originally, after that we do not have
3304# sufficient context to determine whether it is indeed long enough.
3354957a 3305 if ($realfile =~ /Kconfig/ &&
678ae162
UM
3306 # 'choice' is usually the last thing on the line (though
3307 # Kconfig supports named choices), so use a word boundary
3308 # (\b) rather than a whitespace character (\s)
3309 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3354957a 3310 my $length = 0;
9fe287d7
AW
3311 my $cnt = $realcnt;
3312 my $ln = $linenr + 1;
3313 my $f;
a1385803 3314 my $is_start = 0;
9fe287d7 3315 my $is_end = 0;
a1385803 3316 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
9fe287d7
AW
3317 $f = $lines[$ln - 1];
3318 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3319 $is_end = $lines[$ln - 1] =~ /^\+/;
9fe287d7
AW
3320
3321 next if ($f =~ /^-/);
8d73e0e7 3322 last if (!$file && $f =~ /^\@\@/);
a1385803 3323
86adf1a0 3324 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
a1385803 3325 $is_start = 1;
22a4ac02 3326 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
a1385803
AW
3327 $length = -1;
3328 }
3329
9fe287d7 3330 $f =~ s/^.//;
3354957a
AK
3331 $f =~ s/#.*//;
3332 $f =~ s/^\s+//;
3333 next if ($f =~ /^$/);
678ae162
UM
3334
3335 # This only checks context lines in the patch
3336 # and so hopefully shouldn't trigger false
3337 # positives, even though some of these are
3338 # common words in help texts
3339 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3340 if|endif|menu|endmenu|source)\b/x) {
9fe287d7
AW
3341 $is_end = 1;
3342 last;
3343 }
3354957a
AK
3344 $length++;
3345 }
56193274
VB
3346 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3347 WARN("CONFIG_DESCRIPTION",
3348 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3349 }
a1385803 3350 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3354957a
AK
3351 }
3352
7ccf41a8
JP
3353# check MAINTAINERS entries
3354 if ($realfile =~ /^MAINTAINERS$/) {
3355# check MAINTAINERS entries for the right form
3356 if ($rawline =~ /^\+[A-Z]:/ &&
3357 $rawline !~ /^\+[A-Z]:\t\S/) {
3358 if (WARN("MAINTAINERS_STYLE",
3359 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3360 $fix) {
3361 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3362 }
3363 }
3364# check MAINTAINERS entries for the right ordering too
3365 my $preferred_order = 'MRLSWQBCPTFXNK';
3366 if ($rawline =~ /^\+[A-Z]:/ &&
3367 $prevrawline =~ /^[\+ ][A-Z]:/) {
3368 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3369 my $cur = $1;
3370 my $curval = $2;
3371 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3372 my $prev = $1;
3373 my $prevval = $2;
3374 my $curindex = index($preferred_order, $cur);
3375 my $previndex = index($preferred_order, $prev);
3376 if ($curindex < 0) {
3377 WARN("MAINTAINERS_STYLE",
3378 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3379 } else {
3380 if ($previndex >= 0 && $curindex < $previndex) {
3381 WARN("MAINTAINERS_STYLE",
3382 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3383 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3384 ($prev eq 'X' && $cur eq 'X')) &&
3385 ($prevval cmp $curval) > 0) {
3386 WARN("MAINTAINERS_STYLE",
3387 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3388 }
3389 }
628f91a2
JP
3390 }
3391 }
3392
327953e9
CJ
3393# discourage the use of boolean for type definition attributes of Kconfig options
3394 if ($realfile =~ /Kconfig/ &&
3395 $line =~ /^\+\s*\bboolean\b/) {
3396 WARN("CONFIG_TYPE_BOOLEAN",
3397 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3398 }
3399
c68e5878
AL
3400 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3401 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3402 my $flag = $1;
3403 my $replacement = {
3404 'EXTRA_AFLAGS' => 'asflags-y',
3405 'EXTRA_CFLAGS' => 'ccflags-y',
3406 'EXTRA_CPPFLAGS' => 'cppflags-y',
3407 'EXTRA_LDFLAGS' => 'ldflags-y',
3408 };
3409
3410 WARN("DEPRECATED_VARIABLE",
3411 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3412 }
3413
bff5da43 3414# check for DT compatible documentation
7dd05b38
FV
3415 if (defined $root &&
3416 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3417 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3418
bff5da43
RH
3419 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3420
cc93319b 3421 my $dt_path = $root . "/Documentation/devicetree/bindings/";
852d095d 3422 my $vp_file = $dt_path . "vendor-prefixes.yaml";
cc93319b 3423
bff5da43
RH
3424 foreach my $compat (@compats) {
3425 my $compat2 = $compat;
185d566b
RH
3426 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3427 my $compat3 = $compat;
3428 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3429 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
bff5da43
RH
3430 if ( $? >> 8 ) {
3431 WARN("UNDOCUMENTED_DT_STRING",
3432 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3433 }
3434
4fbf32a6
FV
3435 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3436 my $vendor = $1;
852d095d 3437 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
bff5da43
RH
3438 if ( $? >> 8 ) {
3439 WARN("UNDOCUMENTED_DT_STRING",
cc93319b 3440 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
bff5da43
RH
3441 }
3442 }
3443 }
3444
9f3a8992
RH
3445# check for using SPDX license tag at beginning of files
3446 if ($realline == $checklicenseline) {
3447 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3448 $checklicenseline = 2;
3449 } elsif ($rawline =~ /^\+/) {
3450 my $comment = "";
3451 if ($realfile =~ /\.(h|s|S)$/) {
3452 $comment = '/*';
3453 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3454 $comment = '//';
c8df0ab6 3455 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
9f3a8992
RH
3456 $comment = '#';
3457 } elsif ($realfile =~ /\.rst$/) {
3458 $comment = '..';
3459 }
3460
fdf13693
JP
3461# check SPDX comment style for .[chsS] files
3462 if ($realfile =~ /\.[chsS]$/ &&
3463 $rawline =~ /SPDX-License-Identifier:/ &&
ffbce897 3464 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
fdf13693
JP
3465 WARN("SPDX_LICENSE_TAG",
3466 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3467 }
3468
9f3a8992 3469 if ($comment !~ /^$/ &&
ffbce897
JP
3470 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3471 WARN("SPDX_LICENSE_TAG",
3472 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3b6e8ac9 3473 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
ffbce897
JP
3474 my $spdx_license = $1;
3475 if (!is_SPDX_License_valid($spdx_license)) {
3476 WARN("SPDX_LICENSE_TAG",
3477 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3478 }
50c92900
LR
3479 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3480 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3481 my $msg_level = \&WARN;
3482 $msg_level = \&CHK if ($file);
3483 if (&{$msg_level}("SPDX_LICENSE_TAG",
3484
3485 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3486 $fix) {
3487 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3488 }
3489 }
9f3a8992
RH
3490 }
3491 }
3492 }
3493
a0154cdb
JP
3494# check for embedded filenames
3495 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3496 WARN("EMBEDDED_FILENAME",
3497 "It's generally not useful to have the filename in the file\n" . $herecurr);
3498 }
3499
5368df20 3500# check we are in a valid source file if not then ignore this hunk
d6430f71 3501 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
5368df20 3502
a8da38a9
JP
3503# check for using SPDX-License-Identifier on the wrong line number
3504 if ($realline != $checklicenseline &&
3505 $rawline =~ /\bSPDX-License-Identifier:/ &&
3506 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3507 WARN("SPDX_LICENSE_TAG",
3508 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3509 }
3510
47e0c88b
JP
3511# line length limit (with some exclusions)
3512#
3513# There are a few types of lines that may extend beyond $max_line_length:
3514# logging functions like pr_info that end in a string
3515# lines with a single string
3516# #defines that are a single string
2e4bbbc5 3517# lines with an RFC3986 like URL
47e0c88b
JP
3518#
3519# There are 3 different line length message types:
ab1ecabf 3520# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
47e0c88b
JP
3521# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3522# LONG_LINE all other lines longer than $max_line_length
3523#
3524# if LONG_LINE is ignored, the other 2 types are also ignored
3525#
3526
b4749e96 3527 if ($line =~ /^\+/ && $length > $max_line_length) {
47e0c88b
JP
3528 my $msg_type = "LONG_LINE";
3529
3530 # Check the allowed long line types first
3531
3532 # logging functions that end in a string that starts
3533 # before $max_line_length
3534 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3535 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3536 $msg_type = "";
3537
3538 # lines with only strings (w/ possible termination)
3539 # #defines with only strings
3540 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3541 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3542 $msg_type = "";
3543
cc147506
JP
3544 # More special cases
3545 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3546 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
d560a5f8
JP
3547 $msg_type = "";
3548
2e4bbbc5
AB
3549 # URL ($rawline is used in case the URL is in a comment)
3550 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3551 $msg_type = "";
3552
47e0c88b
JP
3553 # Otherwise set the alternate message types
3554
3555 # a comment starts before $max_line_length
3556 } elsif ($line =~ /($;[\s$;]*)$/ &&
3557 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3558 $msg_type = "LONG_LINE_COMMENT"
3559
3560 # a quoted string starts before $max_line_length
3561 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3562 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3563 $msg_type = "LONG_LINE_STRING"
3564 }
3565
3566 if ($msg_type ne "" &&
3567 (show_type("LONG_LINE") || show_type($msg_type))) {
bdc48fa1
JP
3568 my $msg_level = \&WARN;
3569 $msg_level = \&CHK if ($file);
3570 &{$msg_level}($msg_type,
3571 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
47e0c88b 3572 }
0a920b5b
AW
3573 }
3574
8905a67c
AW
3575# check for adding lines without a newline.
3576 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
47ca69b8
TR
3577 if (WARN("MISSING_EOF_NEWLINE",
3578 "adding a line without newline at end of file\n" . $herecurr) &&
3579 $fix) {
3580 fix_delete_line($fixlinenr+1, "No newline at end of file");
3581 }
8905a67c
AW
3582 }
3583
b9ea10d6 3584# check we are in a valid source file C or perl if not then ignore this hunk
de4c924c 3585 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
0a920b5b
AW
3586
3587# at the beginning of a line any tabs must come first and anything
713a09de 3588# more than $tabsize must use tabs.
c2fdda0d
AW
3589 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3590 $rawline =~ /^\+\s* \s*/) {
3591 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
d2c0a235 3592 $rpt_cleaners = 1;
3705ce5b
JP
3593 if (ERROR("CODE_INDENT",
3594 "code indent should use tabs where possible\n" . $herevet) &&
3595 $fix) {
194f66fc 3596 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3705ce5b 3597 }
0a920b5b
AW
3598 }
3599
08e44365
AP
3600# check for space before tabs.
3601 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3602 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3705ce5b
JP
3603 if (WARN("SPACE_BEFORE_TAB",
3604 "please, no space before tabs\n" . $herevet) &&
3605 $fix) {
194f66fc 3606 while ($fixed[$fixlinenr] =~
713a09de 3607 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
194f66fc 3608 while ($fixed[$fixlinenr] =~
c76f4cb3 3609 s/(^\+.*) +\t/$1\t/) {}
3705ce5b 3610 }
08e44365 3611 }
6a487211
JP
3612
3613# check for assignments on the start of a line
3614 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
da7355ab
AS
3615 my $operator = $1;
3616 if (CHK("ASSIGNMENT_CONTINUATIONS",
3617 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3618 $fix && $prevrawline =~ /^\+/) {
3619 # add assignment operator to the previous line, remove from current line
3620 $fixed[$fixlinenr - 1] .= " $operator";
3621 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3622 }
6a487211 3623 }
08e44365 3624
d1fe9c09
JP
3625# check for && or || at the start of a line
3626 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
8e08f076
AS
3627 my $operator = $1;
3628 if (CHK("LOGICAL_CONTINUATIONS",
3629 "Logical continuations should be on the previous line\n" . $hereprev) &&
3630 $fix && $prevrawline =~ /^\+/) {
3631 # insert logical operator at last non-comment, non-whitepsace char on previous line
3632 $prevline =~ /[\s$;]*$/;
3633 my $line_end = substr($prevrawline, $-[0]);
3634 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3635 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3636 }
d1fe9c09
JP
3637 }
3638
a91e8994 3639# check indentation starts on a tab stop
5b57980d 3640 if ($perl_version_ok &&
bd49111f 3641 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
a91e8994 3642 my $indent = length($1);
713a09de 3643 if ($indent % $tabsize) {
a91e8994
JP
3644 if (WARN("TABSTOP",
3645 "Statements should start on a tabstop\n" . $herecurr) &&
3646 $fix) {
713a09de 3647 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
a91e8994
JP
3648 }
3649 }
3650 }
3651
d1fe9c09 3652# check multi-line statement indentation matches previous line
5b57980d 3653 if ($perl_version_ok &&
fd71f632 3654 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
d1fe9c09
JP
3655 $prevline =~ /^\+(\t*)(.*)$/;
3656 my $oldindent = $1;
3657 my $rest = $2;
3658
3659 my $pos = pos_last_openparen($rest);
3660 if ($pos >= 0) {
b34a26f3
JP
3661 $line =~ /^(\+| )([ \t]*)/;
3662 my $newindent = $2;
d1fe9c09
JP
3663
3664 my $goodtabindent = $oldindent .
713a09de
AB
3665 "\t" x ($pos / $tabsize) .
3666 " " x ($pos % $tabsize);
d1fe9c09
JP
3667 my $goodspaceindent = $oldindent . " " x $pos;
3668
3669 if ($newindent ne $goodtabindent &&
3670 $newindent ne $goodspaceindent) {
3705ce5b
JP
3671
3672 if (CHK("PARENTHESIS_ALIGNMENT",
3673 "Alignment should match open parenthesis\n" . $hereprev) &&
3674 $fix && $line =~ /^\+/) {
194f66fc 3675 $fixed[$fixlinenr] =~
3705ce5b
JP
3676 s/^\+[ \t]*/\+$goodtabindent/;
3677 }
d1fe9c09
JP
3678 }
3679 }
3680 }
3681
6ab3a970
JP
3682# check for space after cast like "(int) foo" or "(struct foo) bar"
3683# avoid checking a few false positives:
3684# "sizeof(<type>)" or "__alignof__(<type>)"
3685# function pointer declarations like "(*foo)(int) = bar;"
3686# structure definitions like "(struct foo) { 0 };"
3687# multiline macros that define functions
3688# known attributes or the __attribute__ keyword
3689 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3690 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3705ce5b 3691 if (CHK("SPACING",
f27c95db 3692 "No space is necessary after a cast\n" . $herecurr) &&
3705ce5b 3693 $fix) {
194f66fc 3694 $fixed[$fixlinenr] =~
f27c95db 3695 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3705ce5b 3696 }
aad4f614
JP
3697 }
3698
86406b1c
JP
3699# Block comment styles
3700# Networking with an initial /*
05880600 3701 if ($realfile =~ m@^(drivers/net/|net/)@ &&
fdb4bcd6 3702 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
85ad978c 3703 $rawline =~ /^\+[ \t]*\*/ &&
c70735c2 3704 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
05880600
JP
3705 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3706 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3707 }
3708
86406b1c
JP
3709# Block comments use * on subsequent lines
3710 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3711 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
a605e32e 3712 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
61135e96 3713 $rawline =~ /^\+/ && #line is new
a605e32e 3714 $rawline !~ /^\+[ \t]*\*/) { #no leading *
86406b1c
JP
3715 WARN("BLOCK_COMMENT_STYLE",
3716 "Block comments use * on subsequent lines\n" . $hereprev);
a605e32e
JP
3717 }
3718
86406b1c
JP
3719# Block comments use */ on trailing lines
3720 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
c24f9f19
JP
3721 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3722 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3723 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
86406b1c
JP
3724 WARN("BLOCK_COMMENT_STYLE",
3725 "Block comments use a trailing */ on a separate line\n" . $herecurr);
05880600
JP
3726 }
3727
08eb9b80
JP
3728# Block comment * alignment
3729 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
af207524
JP
3730 $line =~ /^\+[ \t]*$;/ && #leading comment
3731 $rawline =~ /^\+[ \t]*\*/ && #leading *
3732 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
08eb9b80 3733 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
af207524
JP
3734 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3735 my $oldindent;
08eb9b80 3736 $prevrawline =~ m@^\+([ \t]*/?)\*@;
af207524
JP
3737 if (defined($1)) {
3738 $oldindent = expand_tabs($1);
3739 } else {
3740 $prevrawline =~ m@^\+(.*/?)\*@;
3741 $oldindent = expand_tabs($1);
3742 }
08eb9b80
JP
3743 $rawline =~ m@^\+([ \t]*)\*@;
3744 my $newindent = $1;
08eb9b80 3745 $newindent = expand_tabs($newindent);
af207524 3746 if (length($oldindent) ne length($newindent)) {
08eb9b80
JP
3747 WARN("BLOCK_COMMENT_STYLE",
3748 "Block comments should align the * on each line\n" . $hereprev);
3749 }
3750 }
3751
7f619191
JP
3752# check for missing blank lines after struct/union declarations
3753# with exceptions for various attributes and macros
3754 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3755 $line =~ /^\+/ &&
3756 !($line =~ /^\+\s*$/ ||
3757 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3758 $line =~ /^\+\s*MODULE_/i ||
3759 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3760 $line =~ /^\+[a-z_]*init/ ||
3761 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3762 $line =~ /^\+\s*DECLARE/ ||
0bc989ff 3763 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
7f619191 3764 $line =~ /^\+\s*__setup/)) {
d752fcc8
JP
3765 if (CHK("LINE_SPACING",
3766 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3767 $fix) {
f2d7e4d4 3768 fix_insert_line($fixlinenr, "\+");
d752fcc8 3769 }
7f619191
JP
3770 }
3771
365dd4ea
JP
3772# check for multiple consecutive blank lines
3773 if ($prevline =~ /^[\+ ]\s*$/ &&
3774 $line =~ /^\+\s*$/ &&
3775 $last_blank_line != ($linenr - 1)) {
d752fcc8
JP
3776 if (CHK("LINE_SPACING",
3777 "Please don't use multiple blank lines\n" . $hereprev) &&
3778 $fix) {
f2d7e4d4 3779 fix_delete_line($fixlinenr, $rawline);
d752fcc8
JP
3780 }
3781
365dd4ea
JP
3782 $last_blank_line = $linenr;
3783 }
3784
3b617e3b 3785# check for missing blank lines after declarations
3f7bac03
JP
3786 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3787 # actual declarations
3788 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
5a4e1fd3
JP
3789 # function pointer declarations
3790 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3f7bac03
JP
3791 # foo bar; where foo is some local typedef or #define
3792 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3793 # known declaration macros
3794 $prevline =~ /^\+\s+$declaration_macros/) &&
3795 # for "else if" which can look like "$Ident $Ident"
3796 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3797 # other possible extensions of declaration lines
3798 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3799 # not starting a section or a macro "\" extended line
3800 $prevline =~ /(?:\{\s*|\\)$/) &&
3801 # looks like a declaration
3802 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
5a4e1fd3
JP
3803 # function pointer declarations
3804 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3f7bac03
JP
3805 # foo bar; where foo is some local typedef or #define
3806 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3807 # known declaration macros
3808 $sline =~ /^\+\s+$declaration_macros/ ||
3809 # start of struct or union or enum
328b5f41 3810 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
3f7bac03
JP
3811 # start or end of block or continuation of declaration
3812 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3813 # bitfield continuation
3814 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3815 # other possible extensions of declaration lines
3816 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3817 # indentation of previous and current line are the same
3818 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
d752fcc8
JP
3819 if (WARN("LINE_SPACING",
3820 "Missing a blank line after declarations\n" . $hereprev) &&
3821 $fix) {
f2d7e4d4 3822 fix_insert_line($fixlinenr, "\+");
d752fcc8 3823 }
3b617e3b
JP
3824 }
3825
5f7ddae6 3826# check for spaces at the beginning of a line.
6b4c5beb
AW
3827# Exceptions:
3828# 1) within comments
3829# 2) indented preprocessor commands
3830# 3) hanging labels
3705ce5b 3831 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
5f7ddae6 3832 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3705ce5b
JP
3833 if (WARN("LEADING_SPACE",
3834 "please, no spaces at the start of a line\n" . $herevet) &&
3835 $fix) {
194f66fc 3836 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3705ce5b 3837 }
5f7ddae6
RR
3838 }
3839
b9ea10d6
AW
3840# check we are in a valid C source file if not then ignore this hunk
3841 next if ($realfile !~ /\.(h|c)$/);
3842
5751a24e
JP
3843# check for unusual line ending [ or (
3844 if ($line =~ /^\+.*([\[\(])\s*$/) {
3845 CHK("OPEN_ENDED_LINE",
3846 "Lines should not end with a '$1'\n" . $herecurr);
3847 }
3848
4dbed76f
JP
3849# check if this appears to be the start function declaration, save the name
3850 if ($sline =~ /^\+\{\s*$/ &&
3851 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3852 $context_function = $1;
3853 }
3854
3855# check if this appears to be the end of function declaration
3856 if ($sline =~ /^\+\}\s*$/) {
3857 undef $context_function;
3858 }
3859
032a4c0f 3860# check indentation of any line with a bare else
840080a0 3861# (but not if it is a multiple line "if (foo) return bar; else return baz;")
032a4c0f
JP
3862# if the previous line is a break or return and is indented 1 tab more...
3863 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3864 my $tabs = length($1) + 1;
840080a0
JP
3865 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3866 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3867 defined $lines[$linenr] &&
3868 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
032a4c0f
JP
3869 WARN("UNNECESSARY_ELSE",
3870 "else is not generally useful after a break or return\n" . $hereprev);
3871 }
3872 }
3873
c00df19a 3874# check indentation of a line with a break;
dc58bc55
JP
3875# if the previous line is a goto, return or break
3876# and is indented the same # of tabs
c00df19a
JP
3877 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3878 my $tabs = $1;
dc58bc55
JP
3879 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3880 if (WARN("UNNECESSARY_BREAK",
3881 "break is not useful after a $1\n" . $hereprev) &&
3882 $fix) {
3883 fix_delete_line($fixlinenr, $rawline);
3884 }
c00df19a
JP
3885 }
3886 }
3887
c2fdda0d 3888# check for RCS/CVS revision markers
cf655043 3889 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
000d1cc1
JP
3890 WARN("CVS_KEYWORD",
3891 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
c2fdda0d 3892 }
22f2a2ef 3893
56e77d70
JP
3894# check for old HOTPLUG __dev<foo> section markings
3895 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3896 WARN("HOTPLUG_SECTION",
3897 "Using $1 is unnecessary\n" . $herecurr);
3898 }
3899
9c0ca6f9 3900# Check for potential 'bare' types
2b474a1a
AW
3901 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3902 $realline_next);
3e469cdc 3903#print "LINE<$line>\n";
ca819864 3904 if ($linenr > $suppress_statement &&
1b5539b1 3905 $realcnt && $sline =~ /.\s*\S/) {
170d3a22 3906 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
f5fe35dd 3907 ctx_statement_block($linenr, $realcnt, 0);
171ae1a4
AW
3908 $stat =~ s/\n./\n /g;
3909 $cond =~ s/\n./\n /g;
3910
3e469cdc
AW
3911#print "linenr<$linenr> <$stat>\n";
3912 # If this statement has no statement boundaries within
3913 # it there is no point in retrying a statement scan
3914 # until we hit end of it.
3915 my $frag = $stat; $frag =~ s/;+\s*$//;
3916 if ($frag !~ /(?:{|;)/) {
3917#print "skip<$line_nr_next>\n";
3918 $suppress_statement = $line_nr_next;
3919 }
f74bd194 3920
2b474a1a
AW
3921 # Find the real next line.
3922 $realline_next = $line_nr_next;
3923 if (defined $realline_next &&
3924 (!defined $lines[$realline_next - 1] ||
3925 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3926 $realline_next++;
3927 }
3928
171ae1a4
AW
3929 my $s = $stat;
3930 $s =~ s/{.*$//s;
cf655043 3931
c2fdda0d 3932 # Ignore goto labels.
171ae1a4 3933 if ($s =~ /$Ident:\*$/s) {
c2fdda0d
AW
3934
3935 # Ignore functions being called
171ae1a4 3936 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
c2fdda0d 3937
463f2864
AW
3938 } elsif ($s =~ /^.\s*else\b/s) {
3939
c45dcabd 3940 # declarations always start with types
d2506586 3941 } 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) {
c45dcabd
AW
3942 my $type = $1;
3943 $type =~ s/\s+/ /g;
3944 possible($type, "A:" . $s);
3945
8905a67c 3946 # definitions in global scope can only start with types
a6a84062 3947 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
c45dcabd 3948 possible($1, "B:" . $s);
c2fdda0d 3949 }
8905a67c
AW
3950
3951 # any (foo ... *) is a pointer cast, and foo is a type
65863862 3952 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
c45dcabd 3953 possible($1, "C:" . $s);
8905a67c
AW
3954 }
3955
3956 # Check for any sort of function declaration.
3957 # int foo(something bar, other baz);
3958 # void (*store_gdt)(x86_descr_ptr *);
171ae1a4 3959 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
8905a67c 3960 my ($name_len) = length($1);
8905a67c 3961
cf655043 3962 my $ctx = $s;
773647a0 3963 substr($ctx, 0, $name_len + 1, '');
8905a67c 3964 $ctx =~ s/\)[^\)]*$//;
cf655043 3965
8905a67c 3966 for my $arg (split(/\s*,\s*/, $ctx)) {
c45dcabd 3967 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
8905a67c 3968
c45dcabd 3969 possible($1, "D:" . $s);
8905a67c
AW
3970 }
3971 }
9c0ca6f9 3972 }
8905a67c 3973
9c0ca6f9
AW
3974 }
3975
653d4876
AW
3976#
3977# Checks which may be anchored in the context.
3978#
00df344f 3979
653d4876
AW
3980# Check for switch () and associated case and default
3981# statements should be at the same indent.
00df344f
AW
3982 if ($line=~/\bswitch\s*\(.*\)/) {
3983 my $err = '';
3984 my $sep = '';
3985 my @ctx = ctx_block_outer($linenr, $realcnt);
3986 shift(@ctx);
3987 for my $ctx (@ctx) {
3988 my ($clen, $cindent) = line_stats($ctx);
3989 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3990 $indent != $cindent) {
3991 $err .= "$sep$ctx\n";
3992 $sep = '';
3993 } else {
3994 $sep = "[...]\n";
3995 }
3996 }
3997 if ($err ne '') {
000d1cc1
JP
3998 ERROR("SWITCH_CASE_INDENT_LEVEL",
3999 "switch and case should be at the same indent\n$hereline$err");
de7d4f0e
AW
4000 }
4001 }
4002
4003# if/while/etc brace do not go on next line, unless defining a do while loop,
4004# or if that brace on the next line is for something else
0fe3dc2b 4005 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
773647a0
AW
4006 my $pre_ctx = "$1$2";
4007
9c0ca6f9 4008 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
8eef05dd
JP
4009
4010 if ($line =~ /^\+\t{6,}/) {
4011 WARN("DEEP_INDENTATION",
4012 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4013 }
4014
de7d4f0e
AW
4015 my $ctx_cnt = $realcnt - $#ctx - 1;
4016 my $ctx = join("\n", @ctx);
4017
548596d5
AW
4018 my $ctx_ln = $linenr;
4019 my $ctx_skip = $realcnt;
773647a0 4020
548596d5
AW
4021 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4022 defined $lines[$ctx_ln - 1] &&
4023 $lines[$ctx_ln - 1] =~ /^-/)) {
4024 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4025 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
de7d4f0e 4026 $ctx_ln++;
de7d4f0e 4027 }
548596d5 4028
53210168
AW
4029 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4030 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
de7d4f0e 4031
d752fcc8 4032 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
000d1cc1
JP
4033 ERROR("OPEN_BRACE",
4034 "that open brace { should be on the previous line\n" .
01464f30 4035 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
00df344f 4036 }
773647a0
AW
4037 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4038 $ctx =~ /\)\s*\;\s*$/ &&
4039 defined $lines[$ctx_ln - 1])
4040 {
9c0ca6f9
AW
4041 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4042 if ($nindent > $indent) {
000d1cc1
JP
4043 WARN("TRAILING_SEMICOLON",
4044 "trailing semicolon indicates no statements, indent implies otherwise\n" .
01464f30 4045 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
9c0ca6f9
AW
4046 }
4047 }
00df344f
AW
4048 }
4049
4d001e4d 4050# Check relative indent for conditionals and blocks.
f6950a73 4051 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3e469cdc
AW
4052 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4053 ctx_statement_block($linenr, $realcnt, 0)
4054 if (!defined $stat);
4d001e4d
AW
4055 my ($s, $c) = ($stat, $cond);
4056
4057 substr($s, 0, length($c), '');
4058
9f5af480
JP
4059 # remove inline comments
4060 $s =~ s/$;/ /g;
4061 $c =~ s/$;/ /g;
4d001e4d
AW
4062
4063 # Find out how long the conditional actually is.
6f779c18
AW
4064 my @newlines = ($c =~ /\n/gs);
4065 my $cond_lines = 1 + $#newlines;
4d001e4d 4066
9f5af480
JP
4067 # Make sure we remove the line prefixes as we have
4068 # none on the first line, and are going to readd them
4069 # where necessary.
4070 $s =~ s/\n./\n/gs;
4071 while ($s =~ /\n\s+\\\n/) {
4072 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4073 }
4074
4d001e4d
AW
4075 # We want to check the first line inside the block
4076 # starting at the end of the conditional, so remove:
4077 # 1) any blank line termination
4078 # 2) any opening brace { on end of the line
4079 # 3) any do (...) {
4080 my $continuation = 0;
4081 my $check = 0;
4082 $s =~ s/^.*\bdo\b//;
4083 $s =~ s/^\s*{//;
4084 if ($s =~ s/^\s*\\//) {
4085 $continuation = 1;
4086 }
9bd49efe 4087 if ($s =~ s/^\s*?\n//) {
4d001e4d
AW
4088 $check = 1;
4089 $cond_lines++;
4090 }
4091
4092 # Also ignore a loop construct at the end of a
4093 # preprocessor statement.
4094 if (($prevline =~ /^.\s*#\s*define\s/ ||
4095 $prevline =~ /\\\s*$/) && $continuation == 0) {
4096 $check = 0;
4097 }
4098
9bd49efe 4099 my $cond_ptr = -1;
740504c6 4100 $continuation = 0;
9bd49efe
AW
4101 while ($cond_ptr != $cond_lines) {
4102 $cond_ptr = $cond_lines;
4103
f16fa28f
AW
4104 # If we see an #else/#elif then the code
4105 # is not linear.
4106 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4107 $check = 0;
4108 }
4109
9bd49efe
AW
4110 # Ignore:
4111 # 1) blank lines, they should be at 0,
4112 # 2) preprocessor lines, and
4113 # 3) labels.
740504c6
AW
4114 if ($continuation ||
4115 $s =~ /^\s*?\n/ ||
9bd49efe
AW
4116 $s =~ /^\s*#\s*?/ ||
4117 $s =~ /^\s*$Ident\s*:/) {
740504c6 4118 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
30dad6eb
AW
4119 if ($s =~ s/^.*?\n//) {
4120 $cond_lines++;
4121 }
9bd49efe 4122 }
4d001e4d
AW
4123 }
4124
4125 my (undef, $sindent) = line_stats("+" . $s);
4126 my $stat_real = raw_line($linenr, $cond_lines);
4127
4128 # Check if either of these lines are modified, else
4129 # this is not this patch's fault.
4130 if (!defined($stat_real) ||
4131 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4132 $check = 0;
4133 }
4134 if (defined($stat_real) && $cond_lines > 1) {
4135 $stat_real = "[...]\n$stat_real";
4136 }
4137
9bd49efe 4138 #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";
4d001e4d 4139
9f5af480 4140 if ($check && $s ne '' &&
713a09de 4141 (($sindent % $tabsize) != 0 ||
9f5af480 4142 ($sindent < $indent) ||
f6950a73
JP
4143 ($sindent == $indent &&
4144 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
713a09de 4145 ($sindent > $indent + $tabsize))) {
000d1cc1
JP
4146 WARN("SUSPECT_CODE_INDENT",
4147 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
4d001e4d
AW
4148 }
4149 }
4150
6c72ffaa
AW
4151 # Track the 'values' across context and added lines.
4152 my $opline = $line; $opline =~ s/^./ /;
1f65f947
AW
4153 my ($curr_values, $curr_vars) =
4154 annotate_values($opline . "\n", $prev_values);
6c72ffaa 4155 $curr_values = $prev_values . $curr_values;
c2fdda0d
AW
4156 if ($dbg_values) {
4157 my $outline = $opline; $outline =~ s/\t/ /g;
cf655043
AW
4158 print "$linenr > .$outline\n";
4159 print "$linenr > $curr_values\n";
1f65f947 4160 print "$linenr > $curr_vars\n";
c2fdda0d 4161 }
6c72ffaa
AW
4162 $prev_values = substr($curr_values, -1);
4163
00df344f 4164#ignore lines not being added
3705ce5b 4165 next if ($line =~ /^[^\+]/);
00df344f 4166
99ca38c2
JP
4167# check for self assignments used to avoid compiler warnings
4168# e.g.: int foo = foo, *bar = NULL;
4169# struct foo bar = *(&(bar));
4170 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4171 my $var = $1;
4172 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4173 WARN("SELF_ASSIGNMENT",
4174 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4175 }
4176 }
4177
11ca40a0
JP
4178# check for dereferences that span multiple lines
4179 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4180 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4181 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4182 my $ref = $1;
4183 $line =~ /^.\s*($Lval)/;
4184 $ref .= $1;
4185 $ref =~ s/\s//g;
4186 WARN("MULTILINE_DEREFERENCE",
4187 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4188 }
4189
a1ce18e4 4190# check for declarations of signed or unsigned without int
c8447115 4191 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
a1ce18e4
JP
4192 my $type = $1;
4193 my $var = $2;
207a8e84
JP
4194 $var = "" if (!defined $var);
4195 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
a1ce18e4
JP
4196 my $sign = $1;
4197 my $pointer = $2;
4198
4199 $pointer = "" if (!defined $pointer);
4200
4201 if (WARN("UNSPECIFIED_INT",
4202 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4203 $fix) {
4204 my $decl = trim($sign) . " int ";
207a8e84
JP
4205 my $comp_pointer = $pointer;
4206 $comp_pointer =~ s/\s//g;
4207 $decl .= $comp_pointer;
4208 $decl = rtrim($decl) if ($var eq "");
4209 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
a1ce18e4
JP
4210 }
4211 }
4212 }
4213
653d4876 4214# TEST: allow direct testing of the type matcher.
7429c690
AW
4215 if ($dbg_type) {
4216 if ($line =~ /^.\s*$Declare\s*$/) {
000d1cc1
JP
4217 ERROR("TEST_TYPE",
4218 "TEST: is type\n" . $herecurr);
7429c690 4219 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
000d1cc1
JP
4220 ERROR("TEST_NOT_TYPE",
4221 "TEST: is not type ($1 is)\n". $herecurr);
7429c690 4222 }
653d4876
AW
4223 next;
4224 }
a1ef277e
AW
4225# TEST: allow direct testing of the attribute matcher.
4226 if ($dbg_attr) {
9360b0e5 4227 if ($line =~ /^.\s*$Modifier\s*$/) {
000d1cc1
JP
4228 ERROR("TEST_ATTR",
4229 "TEST: is attr\n" . $herecurr);
9360b0e5 4230 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
000d1cc1
JP
4231 ERROR("TEST_NOT_ATTR",
4232 "TEST: is not attr ($1 is)\n". $herecurr);
a1ef277e
AW
4233 }
4234 next;
4235 }
653d4876 4236
f0a594c1 4237# check for initialisation to aggregates open brace on the next line
99423c20
AW
4238 if ($line =~ /^.\s*{/ &&
4239 $prevline =~ /(?:^|[^=])=\s*$/) {
d752fcc8
JP
4240 if (ERROR("OPEN_BRACE",
4241 "that open brace { should be on the previous line\n" . $hereprev) &&
f2d7e4d4
JP
4242 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4243 fix_delete_line($fixlinenr - 1, $prevrawline);
4244 fix_delete_line($fixlinenr, $rawline);
d752fcc8
JP
4245 my $fixedline = $prevrawline;
4246 $fixedline =~ s/\s*=\s*$/ = {/;
f2d7e4d4 4247 fix_insert_line($fixlinenr, $fixedline);
d752fcc8 4248 $fixedline = $line;
8d81ae05 4249 $fixedline =~ s/^(.\s*)\{\s*/$1/;
f2d7e4d4 4250 fix_insert_line($fixlinenr, $fixedline);
d752fcc8 4251 }
f0a594c1
AW
4252 }
4253
653d4876
AW
4254#
4255# Checks which are anchored on the added line.
4256#
4257
4258# check for malformed paths in #include statements (uses RAW line)
c45dcabd 4259 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
653d4876
AW
4260 my $path = $1;
4261 if ($path =~ m{//}) {
000d1cc1 4262 ERROR("MALFORMED_INCLUDE",
495e9d84
JP
4263 "malformed #include filename\n" . $herecurr);
4264 }
4265 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4266 ERROR("UAPI_INCLUDE",
4267 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
653d4876 4268 }
653d4876 4269 }
00df344f 4270
0a920b5b 4271# no C99 // comments
00df344f 4272 if ($line =~ m{//}) {
3705ce5b
JP
4273 if (ERROR("C99_COMMENTS",
4274 "do not use C99 // comments\n" . $herecurr) &&
4275 $fix) {
194f66fc 4276 my $line = $fixed[$fixlinenr];
3705ce5b
JP
4277 if ($line =~ /\/\/(.*)$/) {
4278 my $comment = trim($1);
194f66fc 4279 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3705ce5b
JP
4280 }
4281 }
0a920b5b 4282 }
00df344f 4283 # Remove C99 comments.
0a920b5b 4284 $line =~ s@//.*@@;
6c72ffaa 4285 $opline =~ s@//.*@@;
0a920b5b 4286
2b474a1a
AW
4287# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4288# the whole statement.
4289#print "APW <$lines[$realline_next - 1]>\n";
4290 if (defined $realline_next &&
4291 exists $lines[$realline_next - 1] &&
4292 !defined $suppress_export{$realline_next} &&
4293 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4294 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3cbf62df
AW
4295 # Handle definitions which produce identifiers with
4296 # a prefix:
4297 # XXX(foo);
4298 # EXPORT_SYMBOL(something_foo);
653d4876 4299 my $name = $1;
87a53877 4300 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3cbf62df
AW
4301 $name =~ /^${Ident}_$2/) {
4302#print "FOO C name<$name>\n";
4303 $suppress_export{$realline_next} = 1;
4304
4305 } elsif ($stat !~ /(?:
2b474a1a 4306 \n.}\s*$|
48012058
AW
4307 ^.DEFINE_$Ident\(\Q$name\E\)|
4308 ^.DECLARE_$Ident\(\Q$name\E\)|
4309 ^.LIST_HEAD\(\Q$name\E\)|
2b474a1a
AW
4310 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4311 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
48012058 4312 )/x) {
2b474a1a
AW
4313#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4314 $suppress_export{$realline_next} = 2;
4315 } else {
4316 $suppress_export{$realline_next} = 1;
0a920b5b
AW
4317 }
4318 }
2b474a1a
AW
4319 if (!defined $suppress_export{$linenr} &&
4320 $prevline =~ /^.\s*$/ &&
4321 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4322 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4323#print "FOO B <$lines[$linenr - 1]>\n";
4324 $suppress_export{$linenr} = 2;
4325 }
4326 if (defined $suppress_export{$linenr} &&
4327 $suppress_export{$linenr} == 2) {
000d1cc1
JP
4328 WARN("EXPORT_SYMBOL",
4329 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2b474a1a 4330 }
0a920b5b 4331
5150bda4 4332# check for global initialisers.
6d32f7a3 4333 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
d5e616fc 4334 if (ERROR("GLOBAL_INITIALISERS",
6d32f7a3 4335 "do not initialise globals to $1\n" . $herecurr) &&
d5e616fc 4336 $fix) {
6d32f7a3 4337 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
d5e616fc 4338 }
f0a594c1 4339 }
653d4876 4340# check for static initialisers.
6d32f7a3 4341 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
d5e616fc 4342 if (ERROR("INITIALISED_STATIC",
6d32f7a3 4343 "do not initialise statics to $1\n" .
d5e616fc
JP
4344 $herecurr) &&
4345 $fix) {
6d32f7a3 4346 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
d5e616fc 4347 }
0a920b5b
AW
4348 }
4349
1813087d
JP
4350# check for misordered declarations of char/short/int/long with signed/unsigned
4351 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4352 my $tmp = trim($1);
4353 WARN("MISORDERED_TYPE",
4354 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4355 }
4356
809e082e
JP
4357# check for unnecessary <signed> int declarations of short/long/long long
4358 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4359 my $type = trim($1);
4360 next if ($type !~ /\bint\b/);
4361 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4362 my $new_type = $type;
4363 $new_type =~ s/\b\s*int\s*\b/ /;
4364 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4365 $new_type =~ s/^const\s+//;
4366 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4367 $new_type = "const $new_type" if ($type =~ /^const\b/);
4368 $new_type =~ s/\s+/ /g;
4369 $new_type = trim($new_type);
4370 if (WARN("UNNECESSARY_INT",
4371 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4372 $fix) {
4373 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4374 }
4375 }
4376
cb710eca
JP
4377# check for static const char * arrays.
4378 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
000d1cc1
JP
4379 WARN("STATIC_CONST_CHAR_ARRAY",
4380 "static const char * array should probably be static const char * const\n" .
cb710eca 4381 $herecurr);
77b8c0a8
JP
4382 }
4383
4384# check for initialized const char arrays that should be static const
4385 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4386 if (WARN("STATIC_CONST_CHAR_ARRAY",
4387 "const array should probably be static const\n" . $herecurr) &&
4388 $fix) {
4389 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4390 }
4391 }
cb710eca
JP
4392
4393# check for static char foo[] = "bar" declarations.
4394 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
000d1cc1
JP
4395 WARN("STATIC_CONST_CHAR_ARRAY",
4396 "static char array declaration should probably be static const char\n" .
cb710eca 4397 $herecurr);
77b8c0a8 4398 }
cb710eca 4399
ab7e23f3
JP
4400# check for const <foo> const where <foo> is not a pointer or array type
4401 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4402 my $found = $1;
4403 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4404 WARN("CONST_CONST",
4405 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4406 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4407 WARN("CONST_CONST",
4408 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4409 }
4410 }
4411
73169765
JP
4412# check for const static or static <non ptr type> const declarations
4413# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4414 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4415 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4416 if (WARN("STATIC_CONST",
4417 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4418 $fix) {
4419 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4420 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4421 }
4422 }
4423
9b0fa60d
JP
4424# check for non-global char *foo[] = {"bar", ...} declarations.
4425 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4426 WARN("STATIC_CONST_CHAR_ARRAY",
4427 "char * array declaration might be better as static const\n" .
4428 $herecurr);
4429 }
4430
b598b670
JP
4431# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4432 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4433 my $array = $1;
4434 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4435 my $array_div = $1;
4436 if (WARN("ARRAY_SIZE",
4437 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4438 $fix) {
4439 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4440 }
4441 }
4442 }
4443
b36190c5 4444# check for function declarations without arguments like "int foo()"
16b7f3c8 4445 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
b36190c5
JP
4446 if (ERROR("FUNCTION_WITHOUT_ARGS",
4447 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4448 $fix) {
194f66fc 4449 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
b36190c5
JP
4450 }
4451 }
4452
653d4876
AW
4453# check for new typedefs, only function parameters and sparse annotations
4454# make sense.
4455 if ($line =~ /\btypedef\s/ &&
8054576d 4456 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
c45dcabd 4457 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
8ed22cad 4458 $line !~ /\b$typeTypedefs\b/ &&
46d832f5 4459 $line !~ /\b__bitwise\b/) {
000d1cc1
JP
4460 WARN("NEW_TYPEDEFS",
4461 "do not add new typedefs\n" . $herecurr);
0a920b5b
AW
4462 }
4463
4464# * goes on variable not on type
65863862 4465 # (char*[ const])
bfcb2cc7
AW
4466 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4467 #print "AA<$1>\n";
3705ce5b 4468 my ($ident, $from, $to) = ($1, $2, $2);
65863862
AW
4469
4470 # Should start with a space.
4471 $to =~ s/^(\S)/ $1/;
4472 # Should not end with a space.
4473 $to =~ s/\s+$//;
4474 # '*'s should not have spaces between.
f9a0b3d1 4475 while ($to =~ s/\*\s+\*/\*\*/) {
65863862 4476 }
d8aaf121 4477
3705ce5b 4478## print "1: from<$from> to<$to> ident<$ident>\n";
65863862 4479 if ($from ne $to) {
3705ce5b
JP
4480 if (ERROR("POINTER_LOCATION",
4481 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4482 $fix) {
4483 my $sub_from = $ident;
4484 my $sub_to = $ident;
4485 $sub_to =~ s/\Q$from\E/$to/;
194f66fc 4486 $fixed[$fixlinenr] =~
3705ce5b
JP
4487 s@\Q$sub_from\E@$sub_to@;
4488 }
65863862 4489 }
bfcb2cc7
AW
4490 }
4491 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4492 #print "BB<$1>\n";
3705ce5b 4493 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
65863862
AW
4494
4495 # Should start with a space.
4496 $to =~ s/^(\S)/ $1/;
4497 # Should not end with a space.
4498 $to =~ s/\s+$//;
4499 # '*'s should not have spaces between.
f9a0b3d1 4500 while ($to =~ s/\*\s+\*/\*\*/) {
65863862
AW
4501 }
4502 # Modifiers should have spaces.
4503 $to =~ s/(\b$Modifier$)/$1 /;
d8aaf121 4504
3705ce5b 4505## print "2: from<$from> to<$to> ident<$ident>\n";
667026e7 4506 if ($from ne $to && $ident !~ /^$Modifier$/) {
3705ce5b
JP
4507 if (ERROR("POINTER_LOCATION",
4508 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4509 $fix) {
4510
4511 my $sub_from = $match;
4512 my $sub_to = $match;
4513 $sub_to =~ s/\Q$from\E/$to/;
194f66fc 4514 $fixed[$fixlinenr] =~
3705ce5b
JP
4515 s@\Q$sub_from\E@$sub_to@;
4516 }
65863862 4517 }
0a920b5b
AW
4518 }
4519
9d3e3c70
JP
4520# avoid BUG() or BUG_ON()
4521 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
0675a8fb
JD
4522 my $msg_level = \&WARN;
4523 $msg_level = \&CHK if ($file);
4524 &{$msg_level}("AVOID_BUG",
4525 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
9d3e3c70 4526 }
0a920b5b 4527
9d3e3c70 4528# avoid LINUX_VERSION_CODE
8905a67c 4529 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
000d1cc1
JP
4530 WARN("LINUX_VERSION_CODE",
4531 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
8905a67c
AW
4532 }
4533
17441227
JP
4534# check for uses of printk_ratelimit
4535 if ($line =~ /\bprintk_ratelimit\s*\(/) {
000d1cc1 4536 WARN("PRINTK_RATELIMITED",
101ee680 4537 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
17441227
JP
4538 }
4539
eeef5733
JP
4540# printk should use KERN_* levels
4541 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4542 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4543 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
0a920b5b
AW
4544 }
4545
f5eea3b0
JP
4546# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4547 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4548 my $printk = $1;
4549 my $modifier = $2;
4550 my $orig = $3;
4551 $modifier = "" if (!defined($modifier));
243f3803
JP
4552 my $level = lc($orig);
4553 $level = "warn" if ($level eq "warning");
8f26b837
JP
4554 my $level2 = $level;
4555 $level2 = "dbg" if ($level eq "debug");
f5eea3b0
JP
4556 $level .= $modifier;
4557 $level2 .= $modifier;
243f3803 4558 WARN("PREFER_PR_LEVEL",
f5eea3b0 4559 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr);
243f3803
JP
4560 }
4561
f5eea3b0 4562# prefer dev_<level> to dev_printk(KERN_<LEVEL>
dc139313
JP
4563 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4564 my $orig = $1;
4565 my $level = lc($orig);
4566 $level = "warn" if ($level eq "warning");
4567 $level = "dbg" if ($level eq "debug");
4568 WARN("PREFER_DEV_LEVEL",
4569 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4570 }
4571
8020b253
NB
4572# trace_printk should not be used in production code.
4573 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4574 WARN("TRACE_PRINTK",
4575 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4576 }
4577
91c9afaf
AL
4578# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4579# number of false positives, but assembly files are not checked, so at
4580# least the arch entry code will not trigger this warning.
4581 if ($line =~ /\bENOSYS\b/) {
4582 WARN("ENOSYS",
4583 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4584 }
4585
6b9ea5ff
JK
4586# ENOTSUPP is not a standard error code and should be avoided in new patches.
4587# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4588# Similarly to ENOSYS warning a small number of false positives is expected.
4589 if (!$file && $line =~ /\bENOTSUPP\b/) {
4590 if (WARN("ENOTSUPP",
4591 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4592 $fix) {
4593 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4594 }
4595 }
4596
653d4876
AW
4597# function brace can't be on same line, except for #defines of do while,
4598# or if closed on same line
5b57980d 4599 if ($perl_version_ok &&
2d453e3b
JP
4600 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4601 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4602 $sline !~ /}/) {
8d182478 4603 if (ERROR("OPEN_BRACE",
2d453e3b 4604 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
8d182478
JP
4605 $fix) {
4606 fix_delete_line($fixlinenr, $rawline);
4607 my $fixed_line = $rawline;
03f49351 4608 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
8d182478
JP
4609 my $line1 = $1;
4610 my $line2 = $2;
4611 fix_insert_line($fixlinenr, ltrim($line1));
4612 fix_insert_line($fixlinenr, "\+{");
4613 if ($line2 !~ /^\s*$/) {
4614 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4615 }
4616 }
0a920b5b 4617 }
653d4876 4618
8905a67c
AW
4619# open braces for enum, union and struct go on the same line.
4620 if ($line =~ /^.\s*{/ &&
4621 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
8d182478
JP
4622 if (ERROR("OPEN_BRACE",
4623 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4624 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4625 fix_delete_line($fixlinenr - 1, $prevrawline);
4626 fix_delete_line($fixlinenr, $rawline);
4627 my $fixedline = rtrim($prevrawline) . " {";
4628 fix_insert_line($fixlinenr, $fixedline);
4629 $fixedline = $rawline;
8d81ae05 4630 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
8d182478
JP
4631 if ($fixedline !~ /^\+\s*$/) {
4632 fix_insert_line($fixlinenr, $fixedline);
4633 }
4634 }
8905a67c
AW
4635 }
4636
0c73b4eb 4637# missing space after union, struct or enum definition
3705ce5b
JP
4638 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4639 if (WARN("SPACING",
4640 "missing space after $1 definition\n" . $herecurr) &&
4641 $fix) {
194f66fc 4642 $fixed[$fixlinenr] =~
3705ce5b
JP
4643 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4644 }
0c73b4eb
AW
4645 }
4646
31070b5d
JP
4647# Function pointer declarations
4648# check spacing between type, funcptr, and args
4649# canonical declaration is "type (*funcptr)(args...)"
91f72e9c 4650 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
31070b5d
JP
4651 my $declare = $1;
4652 my $pre_pointer_space = $2;
4653 my $post_pointer_space = $3;
4654 my $funcname = $4;
4655 my $post_funcname_space = $5;
4656 my $pre_args_space = $6;
4657
91f72e9c
JP
4658# the $Declare variable will capture all spaces after the type
4659# so check it for a missing trailing missing space but pointer return types
4660# don't need a space so don't warn for those.
4661 my $post_declare_space = "";
4662 if ($declare =~ /(\s+)$/) {
4663 $post_declare_space = $1;
4664 $declare = rtrim($declare);
4665 }
4666 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
31070b5d
JP
4667 WARN("SPACING",
4668 "missing space after return type\n" . $herecurr);
91f72e9c 4669 $post_declare_space = " ";
31070b5d
JP
4670 }
4671
4672# unnecessary space "type (*funcptr)(args...)"
91f72e9c
JP
4673# This test is not currently implemented because these declarations are
4674# equivalent to
4675# int foo(int bar, ...)
4676# and this is form shouldn't/doesn't generate a checkpatch warning.
4677#
4678# elsif ($declare =~ /\s{2,}$/) {
4679# WARN("SPACING",
4680# "Multiple spaces after return type\n" . $herecurr);
4681# }
31070b5d
JP
4682
4683# unnecessary space "type ( *funcptr)(args...)"
4684 if (defined $pre_pointer_space &&
4685 $pre_pointer_space =~ /^\s/) {
4686 WARN("SPACING",
4687 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4688 }
4689
4690# unnecessary space "type (* funcptr)(args...)"
4691 if (defined $post_pointer_space &&
4692 $post_pointer_space =~ /^\s/) {
4693 WARN("SPACING",
4694 "Unnecessary space before function pointer name\n" . $herecurr);
4695 }
4696
4697# unnecessary space "type (*funcptr )(args...)"
4698 if (defined $post_funcname_space &&
4699 $post_funcname_space =~ /^\s/) {
4700 WARN("SPACING",
4701 "Unnecessary space after function pointer name\n" . $herecurr);
4702 }
4703
4704# unnecessary space "type (*funcptr) (args...)"
4705 if (defined $pre_args_space &&
4706 $pre_args_space =~ /^\s/) {
4707 WARN("SPACING",
4708 "Unnecessary space before function pointer arguments\n" . $herecurr);
4709 }
4710
4711 if (show_type("SPACING") && $fix) {
194f66fc 4712 $fixed[$fixlinenr] =~
91f72e9c 4713 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
31070b5d
JP
4714 }
4715 }
4716
8d31cfce
AW
4717# check for spacing round square brackets; allowed:
4718# 1. with a type on the left -- int [] a;
fe2a7dbc
AW
4719# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4720# 3. inside a curly brace -- = { [0...10] = 5 }
8d31cfce
AW
4721 while ($line =~ /(.*?\s)\[/g) {
4722 my ($where, $prefix) = ($-[1], $1);
4723 if ($prefix !~ /$Type\s+$/ &&
fe2a7dbc 4724 ($where != 0 || $prefix !~ /^.\s+$/) &&
38dca988 4725 $prefix !~ /[{,:]\s+$/) {
3705ce5b
JP
4726 if (ERROR("BRACKET_SPACE",
4727 "space prohibited before open square bracket '['\n" . $herecurr) &&
4728 $fix) {
194f66fc 4729 $fixed[$fixlinenr] =~
3705ce5b
JP
4730 s/^(\+.*?)\s+\[/$1\[/;
4731 }
8d31cfce
AW
4732 }
4733 }
4734
f0a594c1 4735# check for spaces between functions and their parentheses.
6c72ffaa 4736 while ($line =~ /($Ident)\s+\(/g) {
c2fdda0d 4737 my $name = $1;
773647a0
AW
4738 my $ctx_before = substr($line, 0, $-[1]);
4739 my $ctx = "$ctx_before$name";
c2fdda0d
AW
4740
4741 # Ignore those directives where spaces _are_ permitted.
773647a0
AW
4742 if ($name =~ /^(?:
4743 if|for|while|switch|return|case|
4744 volatile|__volatile__|
4745 __attribute__|format|__extension__|
4746 asm|__asm__)$/x)
4747 {
c2fdda0d
AW
4748 # cpp #define statements have non-optional spaces, ie
4749 # if there is a space between the name and the open
4750 # parenthesis it is simply not a parameter group.
c45dcabd 4751 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
773647a0
AW
4752
4753 # cpp #elif statement condition may start with a (
c45dcabd 4754 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
c2fdda0d
AW
4755
4756 # If this whole things ends with a type its most
4757 # likely a typedef for a function.
773647a0 4758 } elsif ($ctx =~ /$Type$/) {
c2fdda0d
AW
4759
4760 } else {
3705ce5b
JP
4761 if (WARN("SPACING",
4762 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4763 $fix) {
194f66fc 4764 $fixed[$fixlinenr] =~
3705ce5b
JP
4765 s/\b$name\s+\(/$name\(/;
4766 }
6c72ffaa 4767 }
f0a594c1 4768 }
9a4cad4e 4769
653d4876 4770# Check operator spacing.
0a920b5b 4771 if (!($line=~/\#\s*include/)) {
3705ce5b
JP
4772 my $fixed_line = "";
4773 my $line_fixed = 0;
4774
9c0ca6f9
AW
4775 my $ops = qr{
4776 <<=|>>=|<=|>=|==|!=|
4777 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4778 =>|->|<<|>>|<|>|=|!|~|
1f65f947 4779 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
84731623 4780 \?:|\?|:
9c0ca6f9 4781 }x;
cf655043 4782 my @elements = split(/($ops|;)/, $opline);
3705ce5b
JP
4783
4784## print("element count: <" . $#elements . ">\n");
4785## foreach my $el (@elements) {
4786## print("el: <$el>\n");
4787## }
4788
4789 my @fix_elements = ();
00df344f 4790 my $off = 0;
6c72ffaa 4791
3705ce5b
JP
4792 foreach my $el (@elements) {
4793 push(@fix_elements, substr($rawline, $off, length($el)));
4794 $off += length($el);
4795 }
4796
4797 $off = 0;
4798
6c72ffaa 4799 my $blank = copy_spacing($opline);
b34c648b 4800 my $last_after = -1;
6c72ffaa 4801
0a920b5b 4802 for (my $n = 0; $n < $#elements; $n += 2) {
3705ce5b
JP
4803
4804 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4805
4806## print("n: <$n> good: <$good>\n");
4807
4a0df2ef
AW
4808 $off += length($elements[$n]);
4809
25985edc 4810 # Pick up the preceding and succeeding characters.
773647a0
AW
4811 my $ca = substr($opline, 0, $off);
4812 my $cc = '';
4813 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4814 $cc = substr($opline, $off + length($elements[$n + 1]));
4815 }
4816 my $cb = "$ca$;$cc";
4817
4a0df2ef
AW
4818 my $a = '';
4819 $a = 'V' if ($elements[$n] ne '');
4820 $a = 'W' if ($elements[$n] =~ /\s$/);
cf655043 4821 $a = 'C' if ($elements[$n] =~ /$;$/);
4a0df2ef
AW
4822 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4823 $a = 'O' if ($elements[$n] eq '');
773647a0 4824 $a = 'E' if ($ca =~ /^\s*$/);
4a0df2ef 4825
0a920b5b 4826 my $op = $elements[$n + 1];
4a0df2ef
AW
4827
4828 my $c = '';
0a920b5b 4829 if (defined $elements[$n + 2]) {
4a0df2ef
AW
4830 $c = 'V' if ($elements[$n + 2] ne '');
4831 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
cf655043 4832 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4a0df2ef
AW
4833 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4834 $c = 'O' if ($elements[$n + 2] eq '');
8b1b3378 4835 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4a0df2ef
AW
4836 } else {
4837 $c = 'E';
0a920b5b
AW
4838 }
4839
4a0df2ef
AW
4840 my $ctx = "${a}x${c}";
4841
4842 my $at = "(ctx:$ctx)";
4843
6c72ffaa 4844 my $ptr = substr($blank, 0, $off) . "^";
de7d4f0e 4845 my $hereptr = "$hereline$ptr\n";
0a920b5b 4846
74048ed8 4847 # Pull out the value of this operator.
6c72ffaa 4848 my $op_type = substr($curr_values, $off + 1, 1);
0a920b5b 4849
1f65f947
AW
4850 # Get the full operator variant.
4851 my $opv = $op . substr($curr_vars, $off, 1);
4852
13214adf
AW
4853 # Ignore operators passed as parameters.
4854 if ($op_type ne 'V' &&
d7fe8065 4855 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
13214adf 4856
cf655043
AW
4857# # Ignore comments
4858# } elsif ($op =~ /^$;+$/) {
13214adf 4859
d8aaf121 4860 # ; should have either the end of line or a space or \ after it
13214adf 4861 } elsif ($op eq ';') {
cf655043
AW
4862 if ($ctx !~ /.x[WEBC]/ &&
4863 $cc !~ /^\\/ && $cc !~ /^;/) {
3705ce5b
JP
4864 if (ERROR("SPACING",
4865 "space required after that '$op' $at\n" . $hereptr)) {
b34c648b 4866 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3705ce5b
JP
4867 $line_fixed = 1;
4868 }
d8aaf121
AW
4869 }
4870
4871 # // is a comment
4872 } elsif ($op eq '//') {
0a920b5b 4873
b00e4814
JP
4874 # : when part of a bitfield
4875 } elsif ($opv eq ':B') {
4876 # skip the bitfield test for now
4877
1f65f947
AW
4878 # No spaces for:
4879 # ->
b00e4814 4880 } elsif ($op eq '->') {
4a0df2ef 4881 if ($ctx =~ /Wx.|.xW/) {
3705ce5b
JP
4882 if (ERROR("SPACING",
4883 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
b34c648b 4884 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3705ce5b
JP
4885 if (defined $fix_elements[$n + 2]) {
4886 $fix_elements[$n + 2] =~ s/^\s+//;
4887 }
b34c648b 4888 $line_fixed = 1;
3705ce5b 4889 }
0a920b5b
AW
4890 }
4891
2381097b 4892 # , must not have a space before and must have a space on the right.
0a920b5b 4893 } elsif ($op eq ',') {
2381097b
JP
4894 my $rtrim_before = 0;
4895 my $space_after = 0;
4896 if ($ctx =~ /Wx./) {
4897 if (ERROR("SPACING",
4898 "space prohibited before that '$op' $at\n" . $hereptr)) {
4899 $line_fixed = 1;
4900 $rtrim_before = 1;
4901 }
4902 }
cf655043 4903 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3705ce5b
JP
4904 if (ERROR("SPACING",
4905 "space required after that '$op' $at\n" . $hereptr)) {
3705ce5b 4906 $line_fixed = 1;
b34c648b 4907 $last_after = $n;
2381097b
JP
4908 $space_after = 1;
4909 }
4910 }
4911 if ($rtrim_before || $space_after) {
4912 if ($rtrim_before) {
4913 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4914 } else {
4915 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4916 }
4917 if ($space_after) {
4918 $good .= " ";
3705ce5b 4919 }
0a920b5b
AW
4920 }
4921
9c0ca6f9 4922 # '*' as part of a type definition -- reported already.
74048ed8 4923 } elsif ($opv eq '*_') {
9c0ca6f9
AW
4924 #warn "'*' is part of type\n";
4925
4926 # unary operators should have a space before and
4927 # none after. May be left adjacent to another
4928 # unary operator, or a cast
4929 } elsif ($op eq '!' || $op eq '~' ||
74048ed8 4930 $opv eq '*U' || $opv eq '-U' ||
0d413866 4931 $opv eq '&U' || $opv eq '&&U') {
cf655043 4932 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3705ce5b
JP
4933 if (ERROR("SPACING",
4934 "space required before that '$op' $at\n" . $hereptr)) {
b34c648b
JP
4935 if ($n != $last_after + 2) {
4936 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4937 $line_fixed = 1;
4938 }
3705ce5b 4939 }
0a920b5b 4940 }
a3340b35 4941 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
171ae1a4
AW
4942 # A unary '*' may be const
4943
4944 } elsif ($ctx =~ /.xW/) {
3705ce5b
JP
4945 if (ERROR("SPACING",
4946 "space prohibited after that '$op' $at\n" . $hereptr)) {
b34c648b 4947 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
3705ce5b
JP
4948 if (defined $fix_elements[$n + 2]) {
4949 $fix_elements[$n + 2] =~ s/^\s+//;
4950 }
b34c648b 4951 $line_fixed = 1;
3705ce5b 4952 }
0a920b5b
AW
4953 }
4954
4955 # unary ++ and unary -- are allowed no space on one side.
4956 } elsif ($op eq '++' or $op eq '--') {
773647a0 4957 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3705ce5b
JP
4958 if (ERROR("SPACING",
4959 "space required one side of that '$op' $at\n" . $hereptr)) {
b34c648b 4960 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3705ce5b
JP
4961 $line_fixed = 1;
4962 }
773647a0
AW
4963 }
4964 if ($ctx =~ /Wx[BE]/ ||
4965 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3705ce5b
JP
4966 if (ERROR("SPACING",
4967 "space prohibited before that '$op' $at\n" . $hereptr)) {
b34c648b 4968 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3705ce5b
JP
4969 $line_fixed = 1;
4970 }
0a920b5b 4971 }
773647a0 4972 if ($ctx =~ /ExW/) {
3705ce5b
JP
4973 if (ERROR("SPACING",
4974 "space prohibited after that '$op' $at\n" . $hereptr)) {
b34c648b 4975 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3705ce5b
JP
4976 if (defined $fix_elements[$n + 2]) {
4977 $fix_elements[$n + 2] =~ s/^\s+//;
4978 }
b34c648b 4979 $line_fixed = 1;
3705ce5b 4980 }
653d4876 4981 }
0a920b5b 4982
0a920b5b 4983 # << and >> may either have or not have spaces both sides
9c0ca6f9
AW
4984 } elsif ($op eq '<<' or $op eq '>>' or
4985 $op eq '&' or $op eq '^' or $op eq '|' or
4986 $op eq '+' or $op eq '-' or
c2fdda0d
AW
4987 $op eq '*' or $op eq '/' or
4988 $op eq '%')
0a920b5b 4989 {
d2e025f3
JP
4990 if ($check) {
4991 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4992 if (CHK("SPACING",
4993 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4994 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4995 $fix_elements[$n + 2] =~ s/^\s+//;
4996 $line_fixed = 1;
4997 }
4998 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4999 if (CHK("SPACING",
5000 "space preferred before that '$op' $at\n" . $hereptr)) {
5001 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5002 $line_fixed = 1;
5003 }
5004 }
5005 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3705ce5b
JP
5006 if (ERROR("SPACING",
5007 "need consistent spacing around '$op' $at\n" . $hereptr)) {
b34c648b
JP
5008 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5009 if (defined $fix_elements[$n + 2]) {
5010 $fix_elements[$n + 2] =~ s/^\s+//;
5011 }
3705ce5b
JP
5012 $line_fixed = 1;
5013 }
0a920b5b
AW
5014 }
5015
1f65f947
AW
5016 # A colon needs no spaces before when it is
5017 # terminating a case value or a label.
5018 } elsif ($opv eq ':C' || $opv eq ':L') {
5019 if ($ctx =~ /Wx./) {
3705ce5b
JP
5020 if (ERROR("SPACING",
5021 "space prohibited before that '$op' $at\n" . $hereptr)) {
b34c648b 5022 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3705ce5b
JP
5023 $line_fixed = 1;
5024 }
1f65f947
AW
5025 }
5026
0a920b5b 5027 # All the others need spaces both sides.
cf655043 5028 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1f65f947
AW
5029 my $ok = 0;
5030
22f2a2ef 5031 # Ignore email addresses <foo@bar>
1f65f947
AW
5032 if (($op eq '<' &&
5033 $cc =~ /^\S+\@\S+>/) ||
5034 ($op eq '>' &&
5035 $ca =~ /<\S+\@\S+$/))
5036 {
342d3d2f 5037 $ok = 1;
1f65f947
AW
5038 }
5039
e0df7e1f
JP
5040 # for asm volatile statements
5041 # ignore a colon with another
5042 # colon immediately before or after
5043 if (($op eq ':') &&
5044 ($ca =~ /:$/ || $cc =~ /^:/)) {
5045 $ok = 1;
5046 }
5047
84731623 5048 # messages are ERROR, but ?: are CHK
1f65f947 5049 if ($ok == 0) {
0675a8fb
JD
5050 my $msg_level = \&ERROR;
5051 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
84731623 5052
0675a8fb
JD
5053 if (&{$msg_level}("SPACING",
5054 "spaces required around that '$op' $at\n" . $hereptr)) {
b34c648b
JP
5055 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5056 if (defined $fix_elements[$n + 2]) {
5057 $fix_elements[$n + 2] =~ s/^\s+//;
5058 }
3705ce5b
JP
5059 $line_fixed = 1;
5060 }
22f2a2ef 5061 }
0a920b5b 5062 }
4a0df2ef 5063 $off += length($elements[$n + 1]);
3705ce5b
JP
5064
5065## print("n: <$n> GOOD: <$good>\n");
5066
5067 $fixed_line = $fixed_line . $good;
5068 }
5069
5070 if (($#elements % 2) == 0) {
5071 $fixed_line = $fixed_line . $fix_elements[$#elements];
0a920b5b 5072 }
3705ce5b 5073
194f66fc
JP
5074 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5075 $fixed[$fixlinenr] = $fixed_line;
3705ce5b
JP
5076 }
5077
5078
0a920b5b
AW
5079 }
5080
786b6326 5081# check for whitespace before a non-naked semicolon
d2e248e7 5082 if ($line =~ /^\+.*\S\s+;\s*$/) {
786b6326
JP
5083 if (WARN("SPACING",
5084 "space prohibited before semicolon\n" . $herecurr) &&
5085 $fix) {
194f66fc 5086 1 while $fixed[$fixlinenr] =~
786b6326
JP
5087 s/^(\+.*\S)\s+;/$1;/;
5088 }
5089 }
5090
f0a594c1
AW
5091# check for multiple assignments
5092 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
000d1cc1
JP
5093 CHK("MULTIPLE_ASSIGNMENTS",
5094 "multiple assignments should be avoided\n" . $herecurr);
f0a594c1
AW
5095 }
5096
22f2a2ef
AW
5097## # check for multiple declarations, allowing for a function declaration
5098## # continuation.
5099## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5100## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5101##
5102## # Remove any bracketed sections to ensure we do not
e73d2715 5103## # falsely report the parameters of functions.
22f2a2ef
AW
5104## my $ln = $line;
5105## while ($ln =~ s/\([^\(\)]*\)//g) {
5106## }
5107## if ($ln =~ /,/) {
000d1cc1
JP
5108## WARN("MULTIPLE_DECLARATION",
5109## "declaring multiple variables together should be avoided\n" . $herecurr);
22f2a2ef
AW
5110## }
5111## }
f0a594c1 5112
0a920b5b 5113#need space before brace following if, while, etc
6b8c69e4 5114 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
6ad724e2 5115 $line =~ /\b(?:else|do)\{/) {
3705ce5b
JP
5116 if (ERROR("SPACING",
5117 "space required before the open brace '{'\n" . $herecurr) &&
5118 $fix) {
6ad724e2 5119 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
3705ce5b 5120 }
de7d4f0e
AW
5121 }
5122
c4a62ef9
JP
5123## # check for blank lines before declarations
5124## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5125## $prevrawline =~ /^.\s*$/) {
5126## WARN("SPACING",
5127## "No blank lines before declarations\n" . $hereprev);
5128## }
5129##
5130
de7d4f0e
AW
5131# closing brace should have a space following it when it has anything
5132# on the line
94fb9845 5133 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
d5e616fc
JP
5134 if (ERROR("SPACING",
5135 "space required after that close brace '}'\n" . $herecurr) &&
5136 $fix) {
194f66fc 5137 $fixed[$fixlinenr] =~
d5e616fc
JP
5138 s/}((?!(?:,|;|\)))\S)/} $1/;
5139 }
0a920b5b
AW
5140 }
5141
22f2a2ef
AW
5142# check spacing on square brackets
5143 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3705ce5b
JP
5144 if (ERROR("SPACING",
5145 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5146 $fix) {
194f66fc 5147 $fixed[$fixlinenr] =~
3705ce5b
JP
5148 s/\[\s+/\[/;
5149 }
22f2a2ef
AW
5150 }
5151 if ($line =~ /\s\]/) {
3705ce5b
JP
5152 if (ERROR("SPACING",
5153 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5154 $fix) {
194f66fc 5155 $fixed[$fixlinenr] =~
3705ce5b
JP
5156 s/\s+\]/\]/;
5157 }
22f2a2ef
AW
5158 }
5159
c45dcabd 5160# check spacing on parentheses
9c0ca6f9
AW
5161 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5162 $line !~ /for\s*\(\s+;/) {
3705ce5b
JP
5163 if (ERROR("SPACING",
5164 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5165 $fix) {
194f66fc 5166 $fixed[$fixlinenr] =~
3705ce5b
JP
5167 s/\(\s+/\(/;
5168 }
22f2a2ef 5169 }
13214adf 5170 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
c45dcabd
AW
5171 $line !~ /for\s*\(.*;\s+\)/ &&
5172 $line !~ /:\s+\)/) {
3705ce5b
JP
5173 if (ERROR("SPACING",
5174 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5175 $fix) {
194f66fc 5176 $fixed[$fixlinenr] =~
3705ce5b
JP
5177 s/\s+\)/\)/;
5178 }
22f2a2ef
AW
5179 }
5180
e2826fd0
JP
5181# check unnecessary parentheses around addressof/dereference single $Lvals
5182# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5183
5184 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
ea4acbb1
JP
5185 my $var = $1;
5186 if (CHK("UNNECESSARY_PARENTHESES",
5187 "Unnecessary parentheses around $var\n" . $herecurr) &&
5188 $fix) {
5189 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5190 }
5191 }
5192
5193# check for unnecessary parentheses around function pointer uses
5194# ie: (foo->bar)(); should be foo->bar();
5195# but not "if (foo->bar) (" to avoid some false positives
5196 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5197 my $var = $2;
5198 if (CHK("UNNECESSARY_PARENTHESES",
5199 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5200 $fix) {
5201 my $var2 = deparenthesize($var);
5202 $var2 =~ s/\s//g;
5203 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5204 }
5205 }
e2826fd0 5206
63b7c73e 5207# check for unnecessary parentheses around comparisons in if uses
a032aa4c
JP
5208# when !drivers/staging or command-line uses --strict
5209 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
5b57980d 5210 $perl_version_ok && defined($stat) &&
63b7c73e
JP
5211 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5212 my $if_stat = $1;
5213 my $test = substr($2, 1, -1);
5214 my $herectx;
5215 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5216 my $match = $1;
5217 # avoid parentheses around potential macro args
5218 next if ($match =~ /^\s*\w+\s*$/);
5219 if (!defined($herectx)) {
5220 $herectx = $here . "\n";
5221 my $cnt = statement_rawlines($if_stat);
5222 for (my $n = 0; $n < $cnt; $n++) {
5223 my $rl = raw_line($linenr, $n);
5224 $herectx .= $rl . "\n";
5225 last if $rl =~ /^[ \+].*\{/;
5226 }
5227 }
5228 CHK("UNNECESSARY_PARENTHESES",
5229 "Unnecessary parentheses around '$match'\n" . $herectx);
5230 }
5231 }
5232
0a920b5b 5233#goto labels aren't indented, allow a single space however
4a0df2ef 5234 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
0a920b5b 5235 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3705ce5b
JP
5236 if (WARN("INDENTED_LABEL",
5237 "labels should not be indented\n" . $herecurr) &&
5238 $fix) {
194f66fc 5239 $fixed[$fixlinenr] =~
3705ce5b
JP
5240 s/^(.)\s+/$1/;
5241 }
0a920b5b
AW
5242 }
5243
40873aba
JP
5244# check if a statement with a comma should be two statements like:
5245# foo = bar(), /* comma should be semicolon */
5246# bar = baz();
5247 if (defined($stat) &&
5248 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5249 my $cnt = statement_rawlines($stat);
5250 my $herectx = get_stat_here($linenr, $cnt, $here);
5251 WARN("SUSPECT_COMMA_SEMICOLON",
5252 "Possible comma where semicolon could be used\n" . $herectx);
5253 }
5254
5b9553ab 5255# return is not a function
507e5141 5256 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
c45dcabd 5257 my $spacing = $1;
5b57980d 5258 if ($perl_version_ok &&
5b9553ab
JP
5259 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5260 my $value = $1;
5261 $value = deparenthesize($value);
5262 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5263 ERROR("RETURN_PARENTHESES",
5264 "return is not a function, parentheses are not required\n" . $herecurr);
5265 }
c45dcabd 5266 } elsif ($spacing !~ /\s+/) {
000d1cc1
JP
5267 ERROR("SPACING",
5268 "space required before the open parenthesis '('\n" . $herecurr);
c45dcabd
AW
5269 }
5270 }
507e5141 5271
b43ae21b
JP
5272# unnecessary return in a void function
5273# at end-of-function, with the previous line a single leading tab, then return;
5274# and the line before that not a goto label target like "out:"
5275 if ($sline =~ /^[ \+]}\s*$/ &&
5276 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5277 $linenr >= 3 &&
5278 $lines[$linenr - 3] =~ /^[ +]/ &&
5279 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
9819cf25 5280 WARN("RETURN_VOID",
b43ae21b
JP
5281 "void function return statements are not generally useful\n" . $hereprev);
5282 }
9819cf25 5283
189248d8 5284# if statements using unnecessary parentheses - ie: if ((foo == bar))
5b57980d 5285 if ($perl_version_ok &&
189248d8
JP
5286 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5287 my $openparens = $1;
5288 my $count = $openparens =~ tr@\(@\(@;
5289 my $msg = "";
5290 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5291 my $comp = $4; #Not $1 because of $LvalOrFunc
5292 $msg = " - maybe == should be = ?" if ($comp eq "==");
5293 WARN("UNNECESSARY_PARENTHESES",
5294 "Unnecessary parentheses$msg\n" . $herecurr);
5295 }
5296 }
5297
c5595fa2
JP
5298# comparisons with a constant or upper case identifier on the left
5299# avoid cases like "foo + BAR < baz"
5300# only fix matches surrounded by parentheses to avoid incorrect
5301# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
5b57980d 5302 if ($perl_version_ok &&
c5595fa2
JP
5303 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5304 my $lead = $1;
5305 my $const = $2;
5306 my $comp = $3;
5307 my $to = $4;
5308 my $newcomp = $comp;
f39e1769 5309 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
c5595fa2
JP
5310 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5311 WARN("CONSTANT_COMPARISON",
5312 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5313 $fix) {
5314 if ($comp eq "<") {
5315 $newcomp = ">";
5316 } elsif ($comp eq "<=") {
5317 $newcomp = ">=";
5318 } elsif ($comp eq ">") {
5319 $newcomp = "<";
5320 } elsif ($comp eq ">=") {
5321 $newcomp = "<=";
5322 }
5323 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5324 }
5325 }
5326
f34e4a4f
JP
5327# Return of what appears to be an errno should normally be negative
5328 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
53a3c448
AW
5329 my $name = $1;
5330 if ($name ne 'EOF' && $name ne 'ERROR') {
000d1cc1 5331 WARN("USE_NEGATIVE_ERRNO",
f34e4a4f 5332 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
53a3c448
AW
5333 }
5334 }
c45dcabd 5335
0a920b5b 5336# Need a space before open parenthesis after if, while etc
3705ce5b
JP
5337 if ($line =~ /\b(if|while|for|switch)\(/) {
5338 if (ERROR("SPACING",
5339 "space required before the open parenthesis '('\n" . $herecurr) &&
5340 $fix) {
194f66fc 5341 $fixed[$fixlinenr] =~
3705ce5b
JP
5342 s/\b(if|while|for|switch)\(/$1 \(/;
5343 }
0a920b5b
AW
5344 }
5345
f5fe35dd
AW
5346# Check for illegal assignment in if conditional -- and check for trailing
5347# statements after the conditional.
170d3a22 5348 if ($line =~ /do\s*(?!{)/) {
3e469cdc
AW
5349 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5350 ctx_statement_block($linenr, $realcnt, 0)
5351 if (!defined $stat);
170d3a22
AW
5352 my ($stat_next) = ctx_statement_block($line_nr_next,
5353 $remain_next, $off_next);
5354 $stat_next =~ s/\n./\n /g;
5355 ##print "stat<$stat> stat_next<$stat_next>\n";
5356
5357 if ($stat_next =~ /^\s*while\b/) {
5358 # If the statement carries leading newlines,
5359 # then count those as offsets.
5360 my ($whitespace) =
5361 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5362 my $offset =
5363 statement_rawlines($whitespace) - 1;
5364
5365 $suppress_whiletrailers{$line_nr_next +
5366 $offset} = 1;
5367 }
5368 }
5369 if (!defined $suppress_whiletrailers{$linenr} &&
c11230f4 5370 defined($stat) && defined($cond) &&
170d3a22 5371 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
171ae1a4 5372 my ($s, $c) = ($stat, $cond);
8905a67c 5373
b53c8e10 5374 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
65b64b3b
JP
5375 if (ERROR("ASSIGN_IN_IF",
5376 "do not use assignment in if condition\n" . $herecurr) &&
5377 $fix && $perl_version_ok) {
5378 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5379 my $space = $1;
5380 my $not = $2;
5381 my $statement = $3;
5382 my $assigned = $4;
5383 my $test = $8;
5384 my $against = $9;
5385 my $brace = $15;
5386 fix_delete_line($fixlinenr, $rawline);
5387 fix_insert_line($fixlinenr, "$space$statement;");
5388 my $newline = "${space}if (";
5389 $newline .= '!' if defined($not);
5390 $newline .= '(' if (defined $not && defined($test) && defined($against));
5391 $newline .= "$assigned";
5392 $newline .= " $test $against" if (defined($test) && defined($against));
5393 $newline .= ')' if (defined $not && defined($test) && defined($against));
5394 $newline .= ')';
5395 $newline .= " {" if (defined($brace));
5396 fix_insert_line($fixlinenr + 1, $newline);
5397 }
5398 }
8905a67c
AW
5399 }
5400
5401 # Find out what is on the end of the line after the
5402 # conditional.
773647a0 5403 substr($s, 0, length($c), '');
8905a67c 5404 $s =~ s/\n.*//g;
342d3d2f 5405 $s =~ s/$;//g; # Remove any comments
53210168
AW
5406 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5407 $c !~ /}\s*while\s*/)
773647a0 5408 {
bb44ad39
AW
5409 # Find out how long the conditional actually is.
5410 my @newlines = ($c =~ /\n/gs);
5411 my $cond_lines = 1 + $#newlines;
42bdf74c 5412 my $stat_real = '';
bb44ad39 5413
42bdf74c
HS
5414 $stat_real = raw_line($linenr, $cond_lines)
5415 . "\n" if ($cond_lines);
bb44ad39
AW
5416 if (defined($stat_real) && $cond_lines > 1) {
5417 $stat_real = "[...]\n$stat_real";
5418 }
5419
000d1cc1
JP
5420 ERROR("TRAILING_STATEMENTS",
5421 "trailing statements should be on next line\n" . $herecurr . $stat_real);
8905a67c
AW
5422 }
5423 }
5424
13214adf
AW
5425# Check for bitwise tests written as boolean
5426 if ($line =~ /
5427 (?:
5428 (?:\[|\(|\&\&|\|\|)
5429 \s*0[xX][0-9]+\s*
5430 (?:\&\&|\|\|)
5431 |
5432 (?:\&\&|\|\|)
5433 \s*0[xX][0-9]+\s*
5434 (?:\&\&|\|\||\)|\])
5435 )/x)
5436 {
000d1cc1
JP
5437 WARN("HEXADECIMAL_BOOLEAN_TEST",
5438 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
13214adf
AW
5439 }
5440
8905a67c 5441# if and else should not have general statements after it
13214adf
AW
5442 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5443 my $s = $1;
342d3d2f 5444 $s =~ s/$;//g; # Remove any comments
13214adf 5445 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
000d1cc1
JP
5446 ERROR("TRAILING_STATEMENTS",
5447 "trailing statements should be on next line\n" . $herecurr);
13214adf 5448 }
0a920b5b 5449 }
39667782
AW
5450# if should not continue a brace
5451 if ($line =~ /}\s*if\b/) {
000d1cc1 5452 ERROR("TRAILING_STATEMENTS",
048b123f 5453 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
39667782
AW
5454 $herecurr);
5455 }
a1080bf8
AW
5456# case and default should not have general statements after them
5457 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5458 $line !~ /\G(?:
3fef12d6 5459 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
a1080bf8
AW
5460 \s*return\s+
5461 )/xg)
5462 {
000d1cc1
JP
5463 ERROR("TRAILING_STATEMENTS",
5464 "trailing statements should be on next line\n" . $herecurr);
a1080bf8 5465 }
0a920b5b
AW
5466
5467 # Check for }<nl>else {, these must be at the same
5468 # indent level to be relevant to each other.
8b8856f4
JP
5469 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5470 $previndent == $indent) {
5471 if (ERROR("ELSE_AFTER_BRACE",
5472 "else should follow close brace '}'\n" . $hereprev) &&
5473 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5474 fix_delete_line($fixlinenr - 1, $prevrawline);
5475 fix_delete_line($fixlinenr, $rawline);
5476 my $fixedline = $prevrawline;
5477 $fixedline =~ s/}\s*$//;
5478 if ($fixedline !~ /^\+\s*$/) {
5479 fix_insert_line($fixlinenr, $fixedline);
5480 }
5481 $fixedline = $rawline;
5482 $fixedline =~ s/^(.\s*)else/$1} else/;
5483 fix_insert_line($fixlinenr, $fixedline);
5484 }
0a920b5b
AW
5485 }
5486
8b8856f4
JP
5487 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5488 $previndent == $indent) {
c2fdda0d
AW
5489 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5490
5491 # Find out what is on the end of the line after the
5492 # conditional.
773647a0 5493 substr($s, 0, length($c), '');
c2fdda0d
AW
5494 $s =~ s/\n.*//g;
5495
5496 if ($s =~ /^\s*;/) {
8b8856f4
JP
5497 if (ERROR("WHILE_AFTER_BRACE",
5498 "while should follow close brace '}'\n" . $hereprev) &&
5499 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5500 fix_delete_line($fixlinenr - 1, $prevrawline);
5501 fix_delete_line($fixlinenr, $rawline);
5502 my $fixedline = $prevrawline;
5503 my $trailing = $rawline;
5504 $trailing =~ s/^\+//;
5505 $trailing = trim($trailing);
5506 $fixedline =~ s/}\s*$/} $trailing/;
5507 fix_insert_line($fixlinenr, $fixedline);
5508 }
c2fdda0d
AW
5509 }
5510 }
5511
95e2c602 5512#Specific variable tests
323c1260
JP
5513 while ($line =~ m{($Constant|$Lval)}g) {
5514 my $var = $1;
95e2c602 5515
95e2c602 5516#CamelCase
807bd26c 5517 if ($var !~ /^$Constant$/ &&
be79794b 5518 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4104a206
ŁS
5519#Ignore some autogenerated defines and enum values
5520 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
22735ce8 5521#Ignore Page<foo> variants
807bd26c 5522 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
d439e6a5
JP
5523#Ignore SI style variants like nS, mV and dB
5524#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5525 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
f5123576
JW
5526#Ignore some three character SI units explicitly, like MiB and KHz
5527 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
7e781f67
JP
5528 while ($var =~ m{($Ident)}g) {
5529 my $word = $1;
5530 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
d8b07710
JP
5531 if ($check) {
5532 seed_camelcase_includes();
5533 if (!$file && !$camelcase_file_seeded) {
5534 seed_camelcase_file($realfile);
5535 $camelcase_file_seeded = 1;
5536 }
5537 }
7e781f67
JP
5538 if (!defined $camelcase{$word}) {
5539 $camelcase{$word} = 1;
5540 CHK("CAMELCASE",
5541 "Avoid CamelCase: <$word>\n" . $herecurr);
5542 }
3445686a 5543 }
323c1260
JP
5544 }
5545 }
0a920b5b
AW
5546
5547#no spaces allowed after \ in define
d5e616fc
JP
5548 if ($line =~ /\#\s*define.*\\\s+$/) {
5549 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5550 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5551 $fix) {
194f66fc 5552 $fixed[$fixlinenr] =~ s/\s+$//;
d5e616fc 5553 }
0a920b5b
AW
5554 }
5555
0e212e0a
FF
5556# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5557# itself <asm/foo.h> (uses RAW line)
c45dcabd 5558 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
e09dec48
AW
5559 my $file = "$1.h";
5560 my $checkfile = "include/linux/$file";
5561 if (-f "$root/$checkfile" &&
5562 $realfile ne $checkfile &&
7840a94c 5563 $1 !~ /$allowed_asm_includes/)
c45dcabd 5564 {
0e212e0a
FF
5565 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5566 if ($asminclude > 0) {
5567 if ($realfile =~ m{^arch/}) {
5568 CHK("ARCH_INCLUDE_LINUX",
5569 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5570 } else {
5571 WARN("INCLUDE_LINUX",
5572 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5573 }
e09dec48 5574 }
0a920b5b
AW
5575 }
5576 }
5577
653d4876
AW
5578# multi-statement macros should be enclosed in a do while loop, grab the
5579# first statement and ensure its the whole macro if its not enclosed
cf655043 5580# in a known good container
b8f96a31
AW
5581 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5582 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
d8aaf121
AW
5583 my $ln = $linenr;
5584 my $cnt = $realcnt;
c45dcabd
AW
5585 my ($off, $dstat, $dcond, $rest);
5586 my $ctx = '';
08a2843e
JP
5587 my $has_flow_statement = 0;
5588 my $has_arg_concat = 0;
c45dcabd 5589 ($dstat, $dcond, $ln, $cnt, $off) =
f74bd194
AW
5590 ctx_statement_block($linenr, $realcnt, 0);
5591 $ctx = $dstat;
c45dcabd 5592 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
a3bb97a7 5593 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
c45dcabd 5594
08a2843e 5595 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
62e15a6d 5596 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
08a2843e 5597
f59b64bf
JP
5598 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5599 my $define_args = $1;
5600 my $define_stmt = $dstat;
5601 my @def_args = ();
5602
5603 if (defined $define_args && $define_args ne "") {
5604 $define_args = substr($define_args, 1, length($define_args) - 2);
5605 $define_args =~ s/\s*//g;
8c8c45cf 5606 $define_args =~ s/\\\+?//g;
f59b64bf
JP
5607 @def_args = split(",", $define_args);
5608 }
5609
292f1a9b 5610 $dstat =~ s/$;//g;
c45dcabd
AW
5611 $dstat =~ s/\\\n.//g;
5612 $dstat =~ s/^\s*//s;
5613 $dstat =~ s/\s*$//s;
de7d4f0e 5614
c45dcabd 5615 # Flatten any parentheses and braces
2e44e803
DR
5616 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5617 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5618 $dstat =~ s/.\[[^\[\]]*\]/1u/)
bf30d6ed 5619 {
de7d4f0e 5620 }
d8aaf121 5621
342d3d2f 5622 # Flatten any obvious string concatenation.
33acb54a
JP
5623 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5624 $dstat =~ s/$Ident\s*($String)/$1/)
e45bab8e
AW
5625 {
5626 }
5627
42e15293
JP
5628 # Make asm volatile uses seem like a generic function
5629 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5630
c45dcabd
AW
5631 my $exceptions = qr{
5632 $Declare|
5633 module_param_named|
a0a0a7a9 5634 MODULE_PARM_DESC|
c45dcabd
AW
5635 DECLARE_PER_CPU|
5636 DEFINE_PER_CPU|
383099fd 5637 __typeof__\(|
22fd2d3e
SS
5638 union|
5639 struct|
ea71a0a0 5640 \.$Ident\s*=\s*|
6b10df42
VZ
5641 ^\"|\"$|
5642 ^\[
c45dcabd 5643 }x;
5eaa20b9 5644 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
f59b64bf
JP
5645
5646 $ctx =~ s/\n*$//;
f59b64bf 5647 my $stmt_cnt = statement_rawlines($ctx);
e3d95a2a 5648 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
f59b64bf 5649
f74bd194
AW
5650 if ($dstat ne '' &&
5651 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5652 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3cc4b1c3 5653 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
356fd398 5654 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
f74bd194
AW
5655 $dstat !~ /$exceptions/ &&
5656 $dstat !~ /^\.$Ident\s*=/ && # .foo =
e942e2c3 5657 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
72f115f9 5658 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
2e44e803 5659 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
f74bd194
AW
5660 $dstat !~ /^for\s*$Constant$/ && # for (...)
5661 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5662 $dstat !~ /^do\s*{/ && # do {...
4e5d56bd 5663 $dstat !~ /^\(\{/ && # ({...
f95a7e6a 5664 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
f74bd194 5665 {
e795556a
JP
5666 if ($dstat =~ /^\s*if\b/) {
5667 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5668 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5669 } elsif ($dstat =~ /;/) {
f74bd194
AW
5670 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5671 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5672 } else {
000d1cc1 5673 ERROR("COMPLEX_MACRO",
388982b5 5674 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
d8aaf121 5675 }
f59b64bf
JP
5676
5677 }
5207649b
JP
5678
5679 # Make $define_stmt single line, comment-free, etc
5680 my @stmt_array = split('\n', $define_stmt);
5681 my $first = 1;
5682 $define_stmt = "";
5683 foreach my $l (@stmt_array) {
5684 $l =~ s/\\$//;
5685 if ($first) {
5686 $define_stmt = $l;
5687 $first = 0;
5688 } elsif ($l =~ /^[\+ ]/) {
5689 $define_stmt .= substr($l, 1);
5690 }
5691 }
5692 $define_stmt =~ s/$;//g;
5693 $define_stmt =~ s/\s+/ /g;
5694 $define_stmt = trim($define_stmt);
5695
f59b64bf
JP
5696# check if any macro arguments are reused (ignore '...' and 'type')
5697 foreach my $arg (@def_args) {
5698 next if ($arg =~ /\.\.\./);
9192d41a 5699 next if ($arg =~ /^type$/i);
7fe528a2 5700 my $tmp_stmt = $define_stmt;
6dba824e 5701 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
7fe528a2
JP
5702 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5703 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
d41362ed 5704 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
f59b64bf
JP
5705 if ($use_cnt > 1) {
5706 CHK("MACRO_ARG_REUSE",
5707 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
9192d41a
JP
5708 }
5709# check if any macro arguments may have other precedence issues
7fe528a2 5710 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
9192d41a
JP
5711 ((defined($1) && $1 ne ',') ||
5712 (defined($2) && $2 ne ','))) {
5713 CHK("MACRO_ARG_PRECEDENCE",
5714 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
f59b64bf 5715 }
653d4876 5716 }
5023d347 5717
08a2843e
JP
5718# check for macros with flow control, but without ## concatenation
5719# ## concatenation is commonly a macro that defines a function so ignore those
5720 if ($has_flow_statement && !$has_arg_concat) {
08a2843e 5721 my $cnt = statement_rawlines($ctx);
e3d95a2a 5722 my $herectx = get_stat_here($linenr, $cnt, $here);
08a2843e 5723
08a2843e
JP
5724 WARN("MACRO_WITH_FLOW_CONTROL",
5725 "Macros with flow control statements should be avoided\n" . "$herectx");
5726 }
5727
481eb486 5728# check for line continuations outside of #defines, preprocessor #, and asm
5023d347
JP
5729
5730 } else {
5731 if ($prevline !~ /^..*\\$/ &&
481eb486
JP
5732 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5733 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5023d347
JP
5734 $line =~ /^\+.*\\$/) {
5735 WARN("LINE_CONTINUATIONS",
5736 "Avoid unnecessary line continuations\n" . $herecurr);
5737 }
0a920b5b
AW
5738 }
5739
b13edf7f
JP
5740# do {} while (0) macro tests:
5741# single-statement macros do not need to be enclosed in do while (0) loop,
5742# macro should not end with a semicolon
5b57980d 5743 if ($perl_version_ok &&
b13edf7f
JP
5744 $realfile !~ m@/vmlinux.lds.h$@ &&
5745 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5746 my $ln = $linenr;
5747 my $cnt = $realcnt;
5748 my ($off, $dstat, $dcond, $rest);
5749 my $ctx = '';
5750 ($dstat, $dcond, $ln, $cnt, $off) =
5751 ctx_statement_block($linenr, $realcnt, 0);
5752 $ctx = $dstat;
5753
5754 $dstat =~ s/\\\n.//g;
1b36b201 5755 $dstat =~ s/$;/ /g;
b13edf7f
JP
5756
5757 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5758 my $stmts = $2;
5759 my $semis = $3;
5760
5761 $ctx =~ s/\n*$//;
5762 my $cnt = statement_rawlines($ctx);
e3d95a2a 5763 my $herectx = get_stat_here($linenr, $cnt, $here);
b13edf7f 5764
ac8e97f8
JP
5765 if (($stmts =~ tr/;/;/) == 1 &&
5766 $stmts !~ /^\s*(if|while|for|switch)\b/) {
b13edf7f
JP
5767 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5768 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5769 }
5770 if (defined $semis && $semis ne "") {
5771 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5772 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5773 }
f5ef95b1
JP
5774 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5775 $ctx =~ s/\n*$//;
5776 my $cnt = statement_rawlines($ctx);
e3d95a2a 5777 my $herectx = get_stat_here($linenr, $cnt, $here);
f5ef95b1
JP
5778
5779 WARN("TRAILING_SEMICOLON",
5780 "macros should not use a trailing semicolon\n" . "$herectx");
b13edf7f
JP
5781 }
5782 }
5783
f0a594c1 5784# check for redundant bracing round if etc
13214adf
AW
5785 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5786 my ($level, $endln, @chunks) =
cf655043 5787 ctx_statement_full($linenr, $realcnt, 1);
13214adf 5788 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
cf655043
AW
5789 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5790 if ($#chunks > 0 && $level == 0) {
aad4f614
JP
5791 my @allowed = ();
5792 my $allow = 0;
13214adf 5793 my $seen = 0;
773647a0 5794 my $herectx = $here . "\n";
cf655043 5795 my $ln = $linenr - 1;
13214adf
AW
5796 for my $chunk (@chunks) {
5797 my ($cond, $block) = @{$chunk};
5798
773647a0
AW
5799 # If the condition carries leading newlines, then count those as offsets.
5800 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5801 my $offset = statement_rawlines($whitespace) - 1;
5802
aad4f614 5803 $allowed[$allow] = 0;
773647a0
AW
5804 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5805
5806 # We have looked at and allowed this specific line.
5807 $suppress_ifbraces{$ln + $offset} = 1;
5808
5809 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
cf655043
AW
5810 $ln += statement_rawlines($block) - 1;
5811
773647a0 5812 substr($block, 0, length($cond), '');
13214adf
AW
5813
5814 $seen++ if ($block =~ /^\s*{/);
5815
aad4f614 5816 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
cf655043
AW
5817 if (statement_lines($cond) > 1) {
5818 #print "APW: ALLOWED: cond<$cond>\n";
aad4f614 5819 $allowed[$allow] = 1;
13214adf
AW
5820 }
5821 if ($block =~/\b(?:if|for|while)\b/) {
cf655043 5822 #print "APW: ALLOWED: block<$block>\n";
aad4f614 5823 $allowed[$allow] = 1;
13214adf 5824 }
cf655043
AW
5825 if (statement_block_size($block) > 1) {
5826 #print "APW: ALLOWED: lines block<$block>\n";
aad4f614 5827 $allowed[$allow] = 1;
13214adf 5828 }
aad4f614 5829 $allow++;
13214adf 5830 }
aad4f614
JP
5831 if ($seen) {
5832 my $sum_allowed = 0;
5833 foreach (@allowed) {
5834 $sum_allowed += $_;
5835 }
5836 if ($sum_allowed == 0) {
5837 WARN("BRACES",
5838 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5839 } elsif ($sum_allowed != $allow &&
5840 $seen != $allow) {
5841 CHK("BRACES",
5842 "braces {} should be used on all arms of this statement\n" . $herectx);
5843 }
13214adf
AW
5844 }
5845 }
5846 }
773647a0 5847 if (!defined $suppress_ifbraces{$linenr - 1} &&
13214adf 5848 $line =~ /\b(if|while|for|else)\b/) {
cf655043
AW
5849 my $allowed = 0;
5850
5851 # Check the pre-context.
5852 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5853 #print "APW: ALLOWED: pre<$1>\n";
5854 $allowed = 1;
5855 }
773647a0
AW
5856
5857 my ($level, $endln, @chunks) =
5858 ctx_statement_full($linenr, $realcnt, $-[0]);
5859
cf655043
AW
5860 # Check the condition.
5861 my ($cond, $block) = @{$chunks[0]};
773647a0 5862 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
cf655043 5863 if (defined $cond) {
773647a0 5864 substr($block, 0, length($cond), '');
cf655043
AW
5865 }
5866 if (statement_lines($cond) > 1) {
5867 #print "APW: ALLOWED: cond<$cond>\n";
5868 $allowed = 1;
5869 }
5870 if ($block =~/\b(?:if|for|while)\b/) {
5871 #print "APW: ALLOWED: block<$block>\n";
5872 $allowed = 1;
5873 }
5874 if (statement_block_size($block) > 1) {
5875 #print "APW: ALLOWED: lines block<$block>\n";
5876 $allowed = 1;
5877 }
5878 # Check the post-context.
5879 if (defined $chunks[1]) {
5880 my ($cond, $block) = @{$chunks[1]};
5881 if (defined $cond) {
773647a0 5882 substr($block, 0, length($cond), '');
cf655043
AW
5883 }
5884 if ($block =~ /^\s*\{/) {
5885 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5886 $allowed = 1;
5887 }
5888 }
5889 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
f055663c 5890 my $cnt = statement_rawlines($block);
e3d95a2a 5891 my $herectx = get_stat_here($linenr, $cnt, $here);
cf655043 5892
000d1cc1
JP
5893 WARN("BRACES",
5894 "braces {} are not necessary for single statement blocks\n" . $herectx);
f0a594c1
AW
5895 }
5896 }
5897
e4c5babd 5898# check for single line unbalanced braces
95330473
SE
5899 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5900 $sline =~ /^.\s*else\s*\{\s*$/) {
e4c5babd
JP
5901 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5902 }
5903
0979ae66 5904# check for unnecessary blank lines around braces
77b9a53a 5905 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
f8e58219
JP
5906 if (CHK("BRACES",
5907 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5908 $fix && $prevrawline =~ /^\+/) {
5909 fix_delete_line($fixlinenr - 1, $prevrawline);
5910 }
0979ae66 5911 }
77b9a53a 5912 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
f8e58219
JP
5913 if (CHK("BRACES",
5914 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5915 $fix) {
5916 fix_delete_line($fixlinenr, $rawline);
5917 }
0979ae66
JP
5918 }
5919
4a0df2ef 5920# no volatiles please
6c72ffaa
AW
5921 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5922 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
000d1cc1 5923 WARN("VOLATILE",
8c27ceff 5924 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
4a0df2ef
AW
5925 }
5926
5e4f6ba5
JP
5927# Check for user-visible strings broken across lines, which breaks the ability
5928# to grep for the string. Make exceptions when the previous string ends in a
5929# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5930# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
33acb54a 5931 if ($line =~ /^\+\s*$String/ &&
5e4f6ba5
JP
5932 $prevline =~ /"\s*$/ &&
5933 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5934 if (WARN("SPLIT_STRING",
5935 "quoted string split across lines\n" . $hereprev) &&
5936 $fix &&
5937 $prevrawline =~ /^\+.*"\s*$/ &&
5938 $last_coalesced_string_linenr != $linenr - 1) {
5939 my $extracted_string = get_quoted_string($line, $rawline);
5940 my $comma_close = "";
5941 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5942 $comma_close = $1;
5943 }
5944
5945 fix_delete_line($fixlinenr - 1, $prevrawline);
5946 fix_delete_line($fixlinenr, $rawline);
5947 my $fixedline = $prevrawline;
5948 $fixedline =~ s/"\s*$//;
5949 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5950 fix_insert_line($fixlinenr - 1, $fixedline);
5951 $fixedline = $rawline;
5952 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5953 if ($fixedline !~ /\+\s*$/) {
5954 fix_insert_line($fixlinenr, $fixedline);
5955 }
5956 $last_coalesced_string_linenr = $linenr;
5957 }
5958 }
5959
5960# check for missing a space in a string concatenation
5961 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5962 WARN('MISSING_SPACE',
5963 "break quoted strings at a space character\n" . $hereprev);
5964 }
5965
e4b7d309
JP
5966# check for an embedded function name in a string when the function is known
5967# This does not work very well for -f --file checking as it depends on patch
5968# context providing the function name or a single line form for in-file
5969# function declarations
77cb8546
JP
5970 if ($line =~ /^\+.*$String/ &&
5971 defined($context_function) &&
e4b7d309
JP
5972 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5973 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
77cb8546 5974 WARN("EMBEDDED_FUNCTION_NAME",
e4b7d309 5975 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
77cb8546
JP
5976 }
5977
5e4f6ba5
JP
5978# check for spaces before a quoted newline
5979 if ($rawline =~ /^.*\".*\s\\n/) {
5980 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5981 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5982 $fix) {
5983 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5984 }
5985
5986 }
5987
f17dba4f 5988# concatenated string without spaces between elements
79682c0c
JP
5989 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5990 if (CHK("CONCATENATED_STRING",
5991 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5992 $fix) {
5993 while ($line =~ /($String)/g) {
5994 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5995 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5996 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5997 }
5998 }
f17dba4f
JP
5999 }
6000
90ad30e5 6001# uncoalesced string fragments
33acb54a 6002 if ($line =~ /$String\s*"/) {
79682c0c
JP
6003 if (WARN("STRING_FRAGMENTS",
6004 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6005 $fix) {
6006 while ($line =~ /($String)(?=\s*")/g) {
6007 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6008 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6009 }
6010 }
90ad30e5
JP
6011 }
6012
522b837c
AD
6013# check for non-standard and hex prefixed decimal printf formats
6014 my $show_L = 1; #don't show the same defect twice
6015 my $show_Z = 1;
5e4f6ba5 6016 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
522b837c 6017 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5e4f6ba5 6018 $string =~ s/%%/__/g;
522b837c
AD
6019 # check for %L
6020 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5e4f6ba5 6021 WARN("PRINTF_L",
522b837c
AD
6022 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6023 $show_L = 0;
6024 }
6025 # check for %Z
6026 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6027 WARN("PRINTF_Z",
6028 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6029 $show_Z = 0;
6030 }
6031 # check for 0x<decimal>
6032 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6033 ERROR("PRINTF_0XDECIMAL",
6e300757
JP
6034 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6035 }
5e4f6ba5
JP
6036 }
6037
6038# check for line continuations in quoted strings with odd counts of "
3f7f335d 6039 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
5e4f6ba5
JP
6040 WARN("LINE_CONTINUATIONS",
6041 "Avoid line continuations in quoted strings\n" . $herecurr);
6042 }
6043
00df344f 6044# warn about #if 0
c45dcabd 6045 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
60f89010
PDH
6046 WARN("IF_0",
6047 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6048 }
6049
6050# warn about #if 1
6051 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6052 WARN("IF_1",
6053 "Consider removing the #if 1 and its #endif\n" . $herecurr);
4a0df2ef
AW
6054 }
6055
03df4b51
AW
6056# check for needless "if (<foo>) fn(<foo>)" uses
6057 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
100425de
JP
6058 my $tested = quotemeta($1);
6059 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6060 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6061 my $func = $1;
6062 if (WARN('NEEDLESS_IF',
6063 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6064 $fix) {
6065 my $do_fix = 1;
6066 my $leading_tabs = "";
6067 my $new_leading_tabs = "";
6068 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6069 $leading_tabs = $1;
6070 } else {
6071 $do_fix = 0;
6072 }
6073 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6074 $new_leading_tabs = $1;
6075 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6076 $do_fix = 0;
6077 }
6078 } else {
6079 $do_fix = 0;
6080 }
6081 if ($do_fix) {
6082 fix_delete_line($fixlinenr - 1, $prevrawline);
6083 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6084 }
6085 }
4c432a8f
GKH
6086 }
6087 }
f0a594c1 6088
ebfdc409
JP
6089# check for unnecessary "Out of Memory" messages
6090 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6091 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6092 (defined $1 || defined $3) &&
6093 $linenr > 3) {
6094 my $testval = $2;
6095 my $testline = $lines[$linenr - 3];
6096
6097 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6098# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6099
e29a70f1
JP
6100 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6101 $s !~ /\b__GFP_NOWARN\b/ ) {
ebfdc409
JP
6102 WARN("OOM_MESSAGE",
6103 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6104 }
6105 }
6106
f78d98f6 6107# check for logging functions with KERN_<LEVEL>
dcaf1123 6108 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
f78d98f6
JP
6109 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6110 my $level = $1;
6111 if (WARN("UNNECESSARY_KERN_LEVEL",
6112 "Possible unnecessary $level\n" . $herecurr) &&
6113 $fix) {
6114 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6115 }
6116 }
6117
45c55e92
JP
6118# check for logging continuations
6119 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6120 WARN("LOGGING_CONTINUATION",
6121 "Avoid logging continuation uses where feasible\n" . $herecurr);
6122 }
6123
70eb2275
DR
6124# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6125 if (defined $stat &&
6126 $line =~ /\b$logFunctions\s*\(/ &&
6127 index($stat, '"') >= 0) {
6128 my $lc = $stat =~ tr@\n@@;
6129 $lc = $lc + $linenr;
6130 my $stat_real = get_stat_real($linenr, $lc);
6131 pos($stat_real) = index($stat_real, '"');
6132 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6133 my $pspec = $1;
6134 my $h = $2;
6135 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6136 if (WARN("UNNECESSARY_MODIFIER",
6137 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6138 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6139 my $nspec = $pspec;
6140 $nspec =~ s/h//g;
6141 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6142 }
6143 }
6144 }
6145
abb08a53 6146# check for mask then right shift without a parentheses
5b57980d 6147 if ($perl_version_ok &&
abb08a53
JP
6148 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6149 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6150 WARN("MASK_THEN_SHIFT",
6151 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6152 }
6153
b75ac618 6154# check for pointer comparisons to NULL
5b57980d 6155 if ($perl_version_ok) {
b75ac618
JP
6156 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6157 my $val = $1;
6158 my $equal = "!";
6159 $equal = "" if ($4 eq "!=");
6160 if (CHK("COMPARISON_TO_NULL",
6161 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6162 $fix) {
6163 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6164 }
6165 }
6166 }
6167
8716de38
JP
6168# check for bad placement of section $InitAttribute (e.g.: __initdata)
6169 if ($line =~ /(\b$InitAttribute\b)/) {
6170 my $attr = $1;
6171 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6172 my $ptr = $1;
6173 my $var = $2;
6174 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6175 ERROR("MISPLACED_INIT",
6176 "$attr should be placed after $var\n" . $herecurr)) ||
6177 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6178 WARN("MISPLACED_INIT",
6179 "$attr should be placed after $var\n" . $herecurr))) &&
6180 $fix) {
194f66fc 6181 $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;
8716de38
JP
6182 }
6183 }
6184 }
6185
e970b884
JP
6186# check for $InitAttributeData (ie: __initdata) with const
6187 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6188 my $attr = $1;
6189 $attr =~ /($InitAttributePrefix)(.*)/;
6190 my $attr_prefix = $1;
6191 my $attr_type = $2;
6192 if (ERROR("INIT_ATTRIBUTE",
6193 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6194 $fix) {
194f66fc 6195 $fixed[$fixlinenr] =~
e970b884
JP
6196 s/$InitAttributeData/${attr_prefix}initconst/;
6197 }
6198 }
6199
6200# check for $InitAttributeConst (ie: __initconst) without const
6201 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6202 my $attr = $1;
6203 if (ERROR("INIT_ATTRIBUTE",
6204 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6205 $fix) {
194f66fc 6206 my $lead = $fixed[$fixlinenr] =~
e970b884
JP
6207 /(^\+\s*(?:static\s+))/;
6208 $lead = rtrim($1);
6209 $lead = "$lead " if ($lead !~ /^\+$/);
6210 $lead = "${lead}const ";
194f66fc 6211 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
e970b884
JP
6212 }
6213 }
6214
c17893c7
JP
6215# check for __read_mostly with const non-pointer (should just be const)
6216 if ($line =~ /\b__read_mostly\b/ &&
6217 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6218 if (ERROR("CONST_READ_MOSTLY",
6219 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6220 $fix) {
6221 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6222 }
6223 }
6224
fbdb8138
JP
6225# don't use __constant_<foo> functions outside of include/uapi/
6226 if ($realfile !~ m@^include/uapi/@ &&
6227 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6228 my $constant_func = $1;
6229 my $func = $constant_func;
6230 $func =~ s/^__constant_//;
6231 if (WARN("CONSTANT_CONVERSION",
6232 "$constant_func should be $func\n" . $herecurr) &&
6233 $fix) {
194f66fc 6234 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
fbdb8138
JP
6235 }
6236 }
6237
1a15a250 6238# prefer usleep_range over udelay
37581c28 6239 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
43c1d77c 6240 my $delay = $1;
1a15a250 6241 # ignore udelay's < 10, however
43c1d77c 6242 if (! ($delay < 10) ) {
000d1cc1 6243 CHK("USLEEP_RANGE",
458f69ef 6244 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
43c1d77c
JP
6245 }
6246 if ($delay > 2000) {
6247 WARN("LONG_UDELAY",
6248 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
1a15a250
PP
6249 }
6250 }
6251
09ef8725
PP
6252# warn about unexpectedly long msleep's
6253 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6254 if ($1 < 20) {
000d1cc1 6255 WARN("MSLEEP",
458f69ef 6256 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
09ef8725
PP
6257 }
6258 }
6259
36ec1939
JP
6260# check for comparisons of jiffies
6261 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6262 WARN("JIFFIES_COMPARISON",
6263 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6264 }
6265
9d7a34a5
JP
6266# check for comparisons of get_jiffies_64()
6267 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6268 WARN("JIFFIES_COMPARISON",
6269 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6270 }
6271
00df344f 6272# warn about #ifdefs in C files
c45dcabd 6273# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
00df344f
AW
6274# print "#ifdef in C files should be avoided\n";
6275# print "$herecurr";
6276# $clean = 0;
6277# }
6278
22f2a2ef 6279# warn about spacing in #ifdefs
c45dcabd 6280 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3705ce5b
JP
6281 if (ERROR("SPACING",
6282 "exactly one space required after that #$1\n" . $herecurr) &&
6283 $fix) {
194f66fc 6284 $fixed[$fixlinenr] =~
3705ce5b
JP
6285 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6286 }
6287
22f2a2ef
AW
6288 }
6289
4a0df2ef 6290# check for spinlock_t definitions without a comment.
171ae1a4
AW
6291 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6292 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4a0df2ef
AW
6293 my $which = $1;
6294 if (!ctx_has_comment($first_line, $linenr)) {
000d1cc1
JP
6295 CHK("UNCOMMENTED_DEFINITION",
6296 "$1 definition without comment\n" . $herecurr);
4a0df2ef
AW
6297 }
6298 }
6299# check for memory barriers without a comment.
402c2553
MT
6300
6301 my $barriers = qr{
6302 mb|
6303 rmb|
ad83ec6c 6304 wmb
402c2553
MT
6305 }x;
6306 my $barrier_stems = qr{
6307 mb__before_atomic|
6308 mb__after_atomic|
6309 store_release|
6310 load_acquire|
6311 store_mb|
6312 (?:$barriers)
6313 }x;
6314 my $all_barriers = qr{
6315 (?:$barriers)|
43e361f2
MT
6316 smp_(?:$barrier_stems)|
6317 virt_(?:$barrier_stems)
402c2553
MT
6318 }x;
6319
6320 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
4a0df2ef 6321 if (!ctx_has_comment($first_line, $linenr)) {
c1fd7bb9
JP
6322 WARN("MEMORY_BARRIER",
6323 "memory barrier without comment\n" . $herecurr);
4a0df2ef
AW
6324 }
6325 }
3ad81779 6326
f4073b0f
MT
6327 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6328
6329 if ($realfile !~ m@^include/asm-generic/@ &&
6330 $realfile !~ m@/barrier\.h$@ &&
6331 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6332 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6333 WARN("MEMORY_BARRIER",
6334 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6335 }
6336
cb426e99
JP
6337# check for waitqueue_active without a comment.
6338 if ($line =~ /\bwaitqueue_active\s*\(/) {
6339 if (!ctx_has_comment($first_line, $linenr)) {
6340 WARN("WAITQUEUE_ACTIVE",
6341 "waitqueue_active without comment\n" . $herecurr);
6342 }
6343 }
3ad81779 6344
5099a722
ME
6345# check for data_race without a comment.
6346 if ($line =~ /\bdata_race\s*\(/) {
6347 if (!ctx_has_comment($first_line, $linenr)) {
6348 WARN("DATA_RACE",
6349 "data_race without comment\n" . $herecurr);
6350 }
6351 }
6352
4a0df2ef 6353# check of hardware specific defines
c45dcabd 6354 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
000d1cc1
JP
6355 CHK("ARCH_DEFINES",
6356 "architecture specific defines should be avoided\n" . $herecurr);
0a920b5b 6357 }
653d4876 6358
596ed45b
JP
6359# check that the storage class is not after a type
6360 if ($line =~ /\b($Type)\s+($Storage)\b/) {
6361 WARN("STORAGE_CLASS",
6362 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6363 }
d4977c78 6364# Check that the storage class is at the beginning of a declaration
596ed45b
JP
6365 if ($line =~ /\b$Storage\b/ &&
6366 $line !~ /^.\s*$Storage/ &&
6367 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6368 $1 !~ /[\,\)]\s*$/) {
000d1cc1 6369 WARN("STORAGE_CLASS",
596ed45b 6370 "storage class should be at the beginning of the declaration\n" . $herecurr);
d4977c78
TK
6371 }
6372
de7d4f0e
AW
6373# check the location of the inline attribute, that it is between
6374# storage class and type.
9c0ca6f9
AW
6375 if ($line =~ /\b$Type\s+$Inline\b/ ||
6376 $line =~ /\b$Inline\s+$Storage\b/) {
000d1cc1
JP
6377 ERROR("INLINE_LOCATION",
6378 "inline keyword should sit between storage class and type\n" . $herecurr);
de7d4f0e
AW
6379 }
6380
8905a67c 6381# Check for __inline__ and __inline, prefer inline
2b7ab453
JP
6382 if ($realfile !~ m@\binclude/uapi/@ &&
6383 $line =~ /\b(__inline__|__inline)\b/) {
d5e616fc
JP
6384 if (WARN("INLINE",
6385 "plain inline is preferred over $1\n" . $herecurr) &&
6386 $fix) {
194f66fc 6387 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
d5e616fc
JP
6388
6389 }
8905a67c
AW
6390 }
6391
7ebe1d17 6392# Check for compiler attributes
2b7ab453 6393 if ($realfile !~ m@\binclude/uapi/@ &&
7ebe1d17
DR
6394 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6395 my $attr = $1;
6396 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
6397
6398 my %attr_list = (
0830aab0 6399 "alias" => "__alias",
7ebe1d17
DR
6400 "aligned" => "__aligned",
6401 "always_inline" => "__always_inline",
6402 "assume_aligned" => "__assume_aligned",
6403 "cold" => "__cold",
6404 "const" => "__attribute_const__",
6405 "copy" => "__copy",
6406 "designated_init" => "__designated_init",
6407 "externally_visible" => "__visible",
6408 "format" => "printf|scanf",
6409 "gnu_inline" => "__gnu_inline",
6410 "malloc" => "__malloc",
6411 "mode" => "__mode",
6412 "no_caller_saved_registers" => "__no_caller_saved_registers",
6413 "noclone" => "__noclone",
6414 "noinline" => "noinline",
6415 "nonstring" => "__nonstring",
6416 "noreturn" => "__noreturn",
6417 "packed" => "__packed",
6418 "pure" => "__pure",
339f29d9 6419 "section" => "__section",
0830aab0
JP
6420 "used" => "__used",
6421 "weak" => "__weak"
7ebe1d17
DR
6422 );
6423
7ebe1d17 6424 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
339f29d9 6425 my $orig_attr = $1;
7ebe1d17
DR
6426 my $params = '';
6427 $params = $2 if defined($2);
339f29d9 6428 my $curr_attr = $orig_attr;
7ebe1d17 6429 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
7ebe1d17 6430 if (exists($attr_list{$curr_attr})) {
339f29d9 6431 my $new = $attr_list{$curr_attr};
7ebe1d17
DR
6432 if ($curr_attr eq "format" && $params) {
6433 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
339f29d9 6434 $new = "__$1\($2";
7ebe1d17 6435 } else {
339f29d9
JP
6436 $new = "$new$params";
6437 }
6438 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6439 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6440 $fix) {
6441 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6442 $fixed[$fixlinenr] =~ s/$remove//;
6443 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6444 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6445 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
7ebe1d17 6446 }
7ebe1d17 6447 }
d5e616fc 6448 }
5f14d3bd 6449
7ebe1d17
DR
6450 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6451 if ($attr =~ /^_*unused/) {
6452 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6453 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
d5e616fc 6454 }
6061d949
JP
6455 }
6456
619a908a 6457# Check for __attribute__ weak, or __weak declarations (may have link issues)
5b57980d 6458 if ($perl_version_ok &&
619a908a
JP
6459 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6460 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6461 $line =~ /\b__weak\b/)) {
6462 ERROR("WEAK_DECLARATION",
6463 "Using weak declarations can have unintended link defects\n" . $herecurr);
6464 }
6465
fd39f904 6466# check for c99 types like uint8_t used outside of uapi/ and tools/
e6176fa4 6467 if ($realfile !~ m@\binclude/uapi/@ &&
fd39f904 6468 $realfile !~ m@\btools/@ &&
e6176fa4
JP
6469 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6470 my $type = $1;
6471 if ($type =~ /\b($typeC99Typedefs)\b/) {
6472 $type = $1;
6473 my $kernel_type = 'u';
6474 $kernel_type = 's' if ($type =~ /^_*[si]/);
6475 $type =~ /(\d+)/;
6476 $kernel_type .= $1;
6477 if (CHK("PREFER_KERNEL_TYPES",
6478 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6479 $fix) {
6480 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6481 }
6482 }
6483 }
6484
938224b5
JP
6485# check for cast of C90 native int or longer types constants
6486 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6487 my $cast = $1;
6488 my $const = $2;
6489 if (WARN("TYPECAST_INT_CONSTANT",
6490 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6491 $fix) {
6492 my $suffix = "";
6493 my $newconst = $const;
6494 $newconst =~ s/${Int_type}$//;
6495 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6496 if ($cast =~ /\blong\s+long\b/) {
6497 $suffix .= 'LL';
6498 } elsif ($cast =~ /\blong\b/) {
6499 $suffix .= 'L';
6500 }
6501 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6502 }
6503 }
6504
8f53a9b8
JP
6505# check for sizeof(&)
6506 if ($line =~ /\bsizeof\s*\(\s*\&/) {
000d1cc1
JP
6507 WARN("SIZEOF_ADDRESS",
6508 "sizeof(& should be avoided\n" . $herecurr);
8f53a9b8
JP
6509 }
6510
66c80b60
JP
6511# check for sizeof without parenthesis
6512 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
d5e616fc
JP
6513 if (WARN("SIZEOF_PARENTHESIS",
6514 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6515 $fix) {
194f66fc 6516 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
d5e616fc 6517 }
66c80b60
JP
6518 }
6519
88982fea
JP
6520# check for struct spinlock declarations
6521 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6522 WARN("USE_SPINLOCK_T",
6523 "struct spinlock should be spinlock_t\n" . $herecurr);
6524 }
6525
a6962d72 6526# check for seq_printf uses that could be seq_puts
06668727 6527 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
a6962d72 6528 my $fmt = get_quoted_string($line, $rawline);
caac1d5f
HA
6529 $fmt =~ s/%%//g;
6530 if ($fmt !~ /%/) {
d5e616fc
JP
6531 if (WARN("PREFER_SEQ_PUTS",
6532 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6533 $fix) {
194f66fc 6534 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
d5e616fc 6535 }
a6962d72
JP
6536 }
6537 }
6538
478b1799 6539# check for vsprintf extension %p<foo> misuses
5b57980d 6540 if ($perl_version_ok &&
0b523769
JP
6541 defined $stat &&
6542 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6543 $1 !~ /^_*volatile_*$/) {
e3c6bc95
TH
6544 my $stat_real;
6545
0b523769
JP
6546 my $lc = $stat =~ tr@\n@@;
6547 $lc = $lc + $linenr;
6548 for (my $count = $linenr; $count <= $lc; $count++) {
ffe07513
JP
6549 my $specifier;
6550 my $extension;
3bd32d6a 6551 my $qualifier;
ffe07513 6552 my $bad_specifier = "";
0b523769
JP
6553 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6554 $fmt =~ s/%%//g;
e3c6bc95 6555
3bd32d6a 6556 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
e3c6bc95
TH
6557 $specifier = $1;
6558 $extension = $2;
3bd32d6a 6559 $qualifier = $3;
361b0d28 6560 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
3bd32d6a
SA
6561 ($extension eq "f" &&
6562 defined $qualifier && $qualifier !~ /^w/)) {
e3c6bc95
TH
6563 $bad_specifier = $specifier;
6564 last;
6565 }
6566 if ($extension eq "x" && !defined($stat_real)) {
6567 if (!defined($stat_real)) {
6568 $stat_real = get_stat_real($linenr, $lc);
6569 }
6570 WARN("VSPRINTF_SPECIFIER_PX",
6571 "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");
6572 }
1df7338a 6573 }
e3c6bc95
TH
6574 if ($bad_specifier ne "") {
6575 my $stat_real = get_stat_real($linenr, $lc);
6576 my $ext_type = "Invalid";
6577 my $use = "";
6578 if ($bad_specifier =~ /p[Ff]/) {
e3c6bc95
TH
6579 $use = " - use %pS instead";
6580 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6581 }
2a9f9d85 6582
e3c6bc95
TH
6583 WARN("VSPRINTF_POINTER_EXTENSION",
6584 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6585 }
0b523769
JP
6586 }
6587 }
6588
554e165c 6589# Check for misused memsets
5b57980d 6590 if ($perl_version_ok &&
d1fe9c09 6591 defined $stat &&
9e20a853 6592 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
d7c76ba7
JP
6593
6594 my $ms_addr = $2;
d1fe9c09
JP
6595 my $ms_val = $7;
6596 my $ms_size = $12;
554e165c 6597
554e165c
AW
6598 if ($ms_size =~ /^(0x|)0$/i) {
6599 ERROR("MEMSET",
d7c76ba7 6600 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
554e165c
AW
6601 } elsif ($ms_size =~ /^(0x|)1$/i) {
6602 WARN("MEMSET",
d7c76ba7
JP
6603 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6604 }
6605 }
6606
98a9bba5 6607# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5b57980d 6608# if ($perl_version_ok &&
f333195d
JP
6609# defined $stat &&
6610# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6611# if (WARN("PREFER_ETHER_ADDR_COPY",
6612# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6613# $fix) {
6614# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6615# }
6616# }
98a9bba5 6617
b6117d17 6618# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5b57980d 6619# if ($perl_version_ok &&
f333195d
JP
6620# defined $stat &&
6621# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6622# WARN("PREFER_ETHER_ADDR_EQUAL",
6623# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6624# }
b6117d17 6625
8617cd09
MK
6626# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6627# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5b57980d 6628# if ($perl_version_ok &&
f333195d
JP
6629# defined $stat &&
6630# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6631#
6632# my $ms_val = $7;
6633#
6634# if ($ms_val =~ /^(?:0x|)0+$/i) {
6635# if (WARN("PREFER_ETH_ZERO_ADDR",
6636# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6637# $fix) {
6638# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6639# }
6640# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6641# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6642# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6643# $fix) {
6644# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6645# }
6646# }
6647# }
8617cd09 6648
d7c76ba7 6649# typecasts on min/max could be min_t/max_t
5b57980d 6650 if ($perl_version_ok &&
d1fe9c09 6651 defined $stat &&
d7c76ba7 6652 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
d1fe9c09 6653 if (defined $2 || defined $7) {
d7c76ba7
JP
6654 my $call = $1;
6655 my $cast1 = deparenthesize($2);
6656 my $arg1 = $3;
d1fe9c09
JP
6657 my $cast2 = deparenthesize($7);
6658 my $arg2 = $8;
d7c76ba7
JP
6659 my $cast;
6660
d1fe9c09 6661 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
d7c76ba7
JP
6662 $cast = "$cast1 or $cast2";
6663 } elsif ($cast1 ne "") {
6664 $cast = $cast1;
6665 } else {
6666 $cast = $cast2;
6667 }
6668 WARN("MINMAX",
6669 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
554e165c
AW
6670 }
6671 }
6672
4a273195 6673# check usleep_range arguments
5b57980d 6674 if ($perl_version_ok &&
4a273195
JP
6675 defined $stat &&
6676 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6677 my $min = $1;
6678 my $max = $7;
6679 if ($min eq $max) {
6680 WARN("USLEEP_RANGE",
458f69ef 6681 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
4a273195
JP
6682 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6683 $min > $max) {
6684 WARN("USLEEP_RANGE",
458f69ef 6685 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
4a273195
JP
6686 }
6687 }
6688
823b794c 6689# check for naked sscanf
5b57980d 6690 if ($perl_version_ok &&
823b794c 6691 defined $stat &&
6c8bd707 6692 $line =~ /\bsscanf\b/ &&
823b794c
JP
6693 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6694 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6695 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6696 my $lc = $stat =~ tr@\n@@;
6697 $lc = $lc + $linenr;
2a9f9d85 6698 my $stat_real = get_stat_real($linenr, $lc);
823b794c
JP
6699 WARN("NAKED_SSCANF",
6700 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6701 }
6702
afc819ab 6703# check for simple sscanf that should be kstrto<foo>
5b57980d 6704 if ($perl_version_ok &&
afc819ab
JP
6705 defined $stat &&
6706 $line =~ /\bsscanf\b/) {
6707 my $lc = $stat =~ tr@\n@@;
6708 $lc = $lc + $linenr;
2a9f9d85 6709 my $stat_real = get_stat_real($linenr, $lc);
afc819ab
JP
6710 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6711 my $format = $6;
6712 my $count = $format =~ tr@%@%@;
6713 if ($count == 1 &&
6714 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6715 WARN("SSCANF_TO_KSTRTO",
6716 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6717 }
6718 }
6719 }
6720
70dc8a48
JP
6721# check for new externs in .h files.
6722 if ($realfile =~ /\.h$/ &&
6723 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
d1d85780
JP
6724 if (CHK("AVOID_EXTERNS",
6725 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
70dc8a48 6726 $fix) {
194f66fc 6727 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
70dc8a48
JP
6728 }
6729 }
6730
de7d4f0e 6731# check for new externs in .c files.
171ae1a4 6732 if ($realfile =~ /\.c$/ && defined $stat &&
c45dcabd 6733 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
171ae1a4 6734 {
c45dcabd
AW
6735 my $function_name = $1;
6736 my $paren_space = $2;
171ae1a4
AW
6737
6738 my $s = $stat;
6739 if (defined $cond) {
6740 substr($s, 0, length($cond), '');
6741 }
d8b44b58 6742 if ($s =~ /^\s*;/)
c45dcabd 6743 {
000d1cc1
JP
6744 WARN("AVOID_EXTERNS",
6745 "externs should be avoided in .c files\n" . $herecurr);
171ae1a4
AW
6746 }
6747
6748 if ($paren_space =~ /\n/) {
000d1cc1
JP
6749 WARN("FUNCTION_ARGUMENTS",
6750 "arguments for function declarations should follow identifier\n" . $herecurr);
171ae1a4 6751 }
9c9ba34e
AW
6752
6753 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6754 $stat =~ /^.\s*extern\s+/)
6755 {
000d1cc1
JP
6756 WARN("AVOID_EXTERNS",
6757 "externs should be avoided in .c files\n" . $herecurr);
de7d4f0e
AW
6758 }
6759
a0ad7596
JP
6760# check for function declarations that have arguments without identifier names
6761 if (defined $stat &&
d8b44b58
KC
6762 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6763 $1 ne "void") {
6764 my $args = trim($1);
ca0d8929
JP
6765 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6766 my $arg = trim($1);
d8b44b58 6767 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
ca0d8929
JP
6768 WARN("FUNCTION_ARGUMENTS",
6769 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6770 }
6771 }
6772 }
6773
a0ad7596 6774# check for function definitions
5b57980d 6775 if ($perl_version_ok &&
a0ad7596
JP
6776 defined $stat &&
6777 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6778 $context_function = $1;
6779
6780# check for multiline function definition with misplaced open brace
6781 my $ok = 0;
6782 my $cnt = statement_rawlines($stat);
6783 my $herectx = $here . "\n";
6784 for (my $n = 0; $n < $cnt; $n++) {
6785 my $rl = raw_line($linenr, $n);
6786 $herectx .= $rl . "\n";
6787 $ok = 1 if ($rl =~ /^[ \+]\{/);
6788 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6789 last if $rl =~ /^[ \+].*\{/;
6790 }
6791 if (!$ok) {
6792 ERROR("OPEN_BRACE",
6793 "open brace '{' following function definitions go on the next line\n" . $herectx);
6794 }
6795 }
6796
de7d4f0e
AW
6797# checks for new __setup's
6798 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6799 my $name = $1;
6800
6801 if (!grep(/$name/, @setup_docs)) {
000d1cc1 6802 CHK("UNDOCUMENTED_SETUP",
2581ac7c 6803 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
de7d4f0e 6804 }
653d4876 6805 }
9c0ca6f9 6806
e29a70f1
JP
6807# check for pointless casting of alloc functions
6808 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
000d1cc1
JP
6809 WARN("UNNECESSARY_CASTS",
6810 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
9c0ca6f9 6811 }
13214adf 6812
a640d25c
JP
6813# alloc style
6814# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5b57980d 6815 if ($perl_version_ok &&
e29a70f1 6816 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
a640d25c
JP
6817 CHK("ALLOC_SIZEOF_STRUCT",
6818 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6819 }
6820
60a55369 6821# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5b57980d 6822 if ($perl_version_ok &&
1b4a2ed4
JP
6823 defined $stat &&
6824 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
60a55369
JP
6825 my $oldfunc = $3;
6826 my $a1 = $4;
6827 my $a2 = $10;
6828 my $newfunc = "kmalloc_array";
6829 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
e367455a
JP
6830 my $r1 = $a1;
6831 my $r2 = $a2;
6832 if ($a1 =~ /^sizeof\s*\S/) {
6833 $r1 = $a2;
6834 $r2 = $a1;
6835 }
6836 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6837 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
1b4a2ed4 6838 my $cnt = statement_rawlines($stat);
e3d95a2a
TH
6839 my $herectx = get_stat_here($linenr, $cnt, $here);
6840
60a55369 6841 if (WARN("ALLOC_WITH_MULTIPLY",
1b4a2ed4
JP
6842 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6843 $cnt == 1 &&
60a55369 6844 $fix) {
194f66fc 6845 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
60a55369
JP
6846 }
6847 }
6848 }
6849
972fdea2 6850# check for krealloc arg reuse
5b57980d 6851 if ($perl_version_ok &&
4cab63ce
JP
6852 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6853 $1 eq $3) {
972fdea2
JP
6854 WARN("KREALLOC_ARG_REUSE",
6855 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6856 }
6857
5ce59ae0
JP
6858# check for alloc argument mismatch
6859 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6860 WARN("ALLOC_ARRAY_ARGS",
6861 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6862 }
6863
caf2a54f
JP
6864# check for multiple semicolons
6865 if ($line =~ /;\s*;\s*$/) {
d5e616fc
JP
6866 if (WARN("ONE_SEMICOLON",
6867 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6868 $fix) {
194f66fc 6869 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
d5e616fc 6870 }
d1e2ad07
JP
6871 }
6872
cec3aaa5
TW
6873# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6874 if ($realfile !~ m@^include/uapi/@ &&
6875 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
0ab90191
JP
6876 my $ull = "";
6877 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6878 if (CHK("BIT_MACRO",
6879 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6880 $fix) {
6881 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6882 }
6883 }
6884
50161266 6885# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
3e89ad85 6886 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
50161266 6887 WARN("IS_ENABLED_CONFIG",
3e89ad85 6888 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
50161266
JP
6889 }
6890
2d632745 6891# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
3e89ad85 6892 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
2d632745
JP
6893 my $config = $1;
6894 if (WARN("PREFER_IS_ENABLED",
3e89ad85 6895 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
2d632745
JP
6896 $fix) {
6897 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6898 }
6899 }
6900
f36d3eb8
JP
6901# check for /* fallthrough */ like comment, prefer fallthrough;
6902 my @fallthroughs = (
6903 'fallthrough',
6904 '@fallthrough@',
6905 'lint -fallthrough[ \t]*',
6906 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6907 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6908 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6909 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6910 );
6911 if ($raw_comment ne '') {
6912 foreach my $ft (@fallthroughs) {
6913 if ($raw_comment =~ /$ft/) {
6914 my $msg_level = \&WARN;
6915 $msg_level = \&CHK if ($file);
6916 &{$msg_level}("PREFER_FALLTHROUGH",
6917 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6918 last;
6919 }
6920 }
6921 }
6922
d1e2ad07 6923# check for switch/default statements without a break;
5b57980d 6924 if ($perl_version_ok &&
d1e2ad07
JP
6925 defined $stat &&
6926 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
d1e2ad07 6927 my $cnt = statement_rawlines($stat);
e3d95a2a
TH
6928 my $herectx = get_stat_here($linenr, $cnt, $here);
6929
d1e2ad07
JP
6930 WARN("DEFAULT_NO_BREAK",
6931 "switch default: should use break\n" . $herectx);
caf2a54f
JP
6932 }
6933
13214adf 6934# check for gcc specific __FUNCTION__
d5e616fc
JP
6935 if ($line =~ /\b__FUNCTION__\b/) {
6936 if (WARN("USE_FUNC",
6937 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6938 $fix) {
194f66fc 6939 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
d5e616fc 6940 }
13214adf 6941 }
773647a0 6942
62ec818f
JP
6943# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6944 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6945 ERROR("DATE_TIME",
6946 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6947 }
6948
2c92488a
JP
6949# check for use of yield()
6950 if ($line =~ /\byield\s*\(\s*\)/) {
6951 WARN("YIELD",
6952 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6953 }
6954
179f8f40
JP
6955# check for comparisons against true and false
6956 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6957 my $lead = $1;
6958 my $arg = $2;
6959 my $test = $3;
6960 my $otype = $4;
6961 my $trail = $5;
6962 my $op = "!";
6963
6964 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6965
6966 my $type = lc($otype);
6967 if ($type =~ /^(?:true|false)$/) {
6968 if (("$test" eq "==" && "$type" eq "true") ||
6969 ("$test" eq "!=" && "$type" eq "false")) {
6970 $op = "";
6971 }
6972
6973 CHK("BOOL_COMPARISON",
6974 "Using comparison to $otype is error prone\n" . $herecurr);
6975
6976## maybe suggesting a correct construct would better
6977## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6978
6979 }
6980 }
6981
4882720b
TG
6982# check for semaphores initialized locked
6983 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
000d1cc1
JP
6984 WARN("CONSIDER_COMPLETION",
6985 "consider using a completion\n" . $herecurr);
773647a0 6986 }
6712d858 6987
67d0a075
JP
6988# recommend kstrto* over simple_strto* and strict_strto*
6989 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
000d1cc1 6990 WARN("CONSIDER_KSTRTO",
67d0a075 6991 "$1 is obsolete, use k$3 instead\n" . $herecurr);
773647a0 6992 }
6712d858 6993
ae3ccc46 6994# check for __initcall(), use device_initcall() explicitly or more appropriate function please
f3db6639 6995 if ($line =~ /^.\s*__initcall\s*\(/) {
000d1cc1 6996 WARN("USE_DEVICE_INITCALL",
ae3ccc46 6997 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
f3db6639 6998 }
6712d858 6999
3d709ab5
PM
7000# check for spin_is_locked(), suggest lockdep instead
7001 if ($line =~ /\bspin_is_locked\(/) {
7002 WARN("USE_LOCKDEP",
7003 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7004 }
7005
9189c7e7
JP
7006# check for deprecated apis
7007 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7008 my $deprecated_api = $1;
7009 my $new_api = $deprecated_apis{$deprecated_api};
7010 WARN("DEPRECATED_API",
7011 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7012 }
7013
0f3c5aab 7014# check for various structs that are normally const (ops, kgdb, device_tree)
d9190e4e 7015# and avoid what seem like struct definitions 'struct foo {'
ced69da1
QM
7016 if (defined($const_structs) &&
7017 $line !~ /\bconst\b/ &&
d9190e4e 7018 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
000d1cc1 7019 WARN("CONST_STRUCT",
d9190e4e 7020 "struct $1 should normally be const\n" . $herecurr);
2b6db5cb 7021 }
773647a0
AW
7022
7023# use of NR_CPUS is usually wrong
7024# ignore definitions of NR_CPUS and usage to define arrays as likely right
7025 if ($line =~ /\bNR_CPUS\b/ &&
c45dcabd
AW
7026 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7027 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
171ae1a4
AW
7028 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7029 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7030 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
773647a0 7031 {
000d1cc1
JP
7032 WARN("NR_CPUS",
7033 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
773647a0 7034 }
9c9ba34e 7035
52ea8506
JP
7036# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7037 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7038 ERROR("DEFINE_ARCH_HAS",
7039 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7040 }
7041
acd9362c 7042# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5b57980d 7043 if ($perl_version_ok &&
acd9362c
JP
7044 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7045 WARN("LIKELY_MISUSE",
7046 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7047 }
7048
de3f186f
DE
7049# nested likely/unlikely calls
7050 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7051 WARN("LIKELY_MISUSE",
7052 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7053 }
7054
691d77b6
AW
7055# whine mightly about in_atomic
7056 if ($line =~ /\bin_atomic\s*\(/) {
7057 if ($realfile =~ m@^drivers/@) {
000d1cc1
JP
7058 ERROR("IN_ATOMIC",
7059 "do not use in_atomic in drivers\n" . $herecurr);
f4a87736 7060 } elsif ($realfile !~ m@^kernel/@) {
000d1cc1
JP
7061 WARN("IN_ATOMIC",
7062 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
691d77b6
AW
7063 }
7064 }
1704f47b 7065
0f5225b0
PZ
7066# check for mutex_trylock_recursive usage
7067 if ($line =~ /mutex_trylock_recursive/) {
7068 ERROR("LOCKING",
7069 "recursive locking is bad, do not use this ever.\n" . $herecurr);
7070 }
7071
1704f47b
PZ
7072# check for lockdep_set_novalidate_class
7073 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7074 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7075 if ($realfile !~ m@^kernel/lockdep@ &&
7076 $realfile !~ m@^include/linux/lockdep@ &&
7077 $realfile !~ m@^drivers/base/core@) {
000d1cc1
JP
7078 ERROR("LOCKDEP",
7079 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
1704f47b
PZ
7080 }
7081 }
88f8831c 7082
b392c64f
JP
7083 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7084 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
000d1cc1
JP
7085 WARN("EXPORTED_WORLD_WRITABLE",
7086 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
88f8831c 7087 }
2435880f 7088
00180468
JP
7089# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7090# and whether or not function naming is typical and if
7091# DEVICE_ATTR permissions uses are unusual too
5b57980d 7092 if ($perl_version_ok &&
00180468
JP
7093 defined $stat &&
7094 $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*\)/) {
7095 my $var = $1;
7096 my $perms = $2;
7097 my $show = $3;
7098 my $store = $4;
7099 my $octal_perms = perms_to_octal($perms);
7100 if ($show =~ /^${var}_show$/ &&
7101 $store =~ /^${var}_store$/ &&
7102 $octal_perms eq "0644") {
7103 if (WARN("DEVICE_ATTR_RW",
7104 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7105 $fix) {
7106 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7107 }
7108 } elsif ($show =~ /^${var}_show$/ &&
7109 $store =~ /^NULL$/ &&
7110 $octal_perms eq "0444") {
7111 if (WARN("DEVICE_ATTR_RO",
7112 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7113 $fix) {
7114 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7115 }
7116 } elsif ($show =~ /^NULL$/ &&
7117 $store =~ /^${var}_store$/ &&
7118 $octal_perms eq "0200") {
7119 if (WARN("DEVICE_ATTR_WO",
7120 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7121 $fix) {
7122 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7123 }
7124 } elsif ($octal_perms eq "0644" ||
7125 $octal_perms eq "0444" ||
7126 $octal_perms eq "0200") {
7127 my $newshow = "$show";
7128 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7129 my $newstore = $store;
7130 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7131 my $rename = "";
7132 if ($show ne $newshow) {
7133 $rename .= " '$show' to '$newshow'";
7134 }
7135 if ($store ne $newstore) {
7136 $rename .= " '$store' to '$newstore'";
7137 }
7138 WARN("DEVICE_ATTR_FUNCTIONS",
7139 "Consider renaming function(s)$rename\n" . $herecurr);
7140 } else {
7141 WARN("DEVICE_ATTR_PERMS",
7142 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7143 }
7144 }
7145
515a235e
JP
7146# Mode permission misuses where it seems decimal should be octal
7147# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
73121534
JP
7148# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7149# specific definition of not visible in sysfs.
7150# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7151# use the default permissions
5b57980d 7152 if ($perl_version_ok &&
459cf0ae 7153 defined $stat &&
515a235e
JP
7154 $line =~ /$mode_perms_search/) {
7155 foreach my $entry (@mode_permission_funcs) {
7156 my $func = $entry->[0];
7157 my $arg_pos = $entry->[1];
7158
459cf0ae
JP
7159 my $lc = $stat =~ tr@\n@@;
7160 $lc = $lc + $linenr;
2a9f9d85 7161 my $stat_real = get_stat_real($linenr, $lc);
459cf0ae 7162
515a235e
JP
7163 my $skip_args = "";
7164 if ($arg_pos > 1) {
7165 $arg_pos--;
7166 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7167 }
f90774e1 7168 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
459cf0ae 7169 if ($stat =~ /$test/) {
515a235e
JP
7170 my $val = $1;
7171 $val = $6 if ($skip_args ne "");
73121534
JP
7172 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7173 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7174 ($val =~ /^$Octal$/ && length($val) ne 4))) {
515a235e 7175 ERROR("NON_OCTAL_PERMISSIONS",
459cf0ae 7176 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
f90774e1
JP
7177 }
7178 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
c0a5c898 7179 ERROR("EXPORTED_WORLD_WRITABLE",
459cf0ae 7180 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
f90774e1 7181 }
2435880f
JP
7182 }
7183 }
7184 }
5a6d20ce 7185
459cf0ae 7186# check for uses of S_<PERMS> that could be octal for readability
bc22d9a7 7187 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
00180468
JP
7188 my $oval = $1;
7189 my $octal = perms_to_octal($oval);
459cf0ae
JP
7190 if (WARN("SYMBOLIC_PERMS",
7191 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7192 $fix) {
00180468 7193 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
459cf0ae
JP
7194 }
7195 }
7196
5a6d20ce
BA
7197# validate content of MODULE_LICENSE against list from include/linux/module.h
7198 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7199 my $extracted_string = get_quoted_string($line, $rawline);
7200 my $valid_licenses = qr{
7201 GPL|
7202 GPL\ v2|
7203 GPL\ and\ additional\ rights|
7204 Dual\ BSD/GPL|
7205 Dual\ MIT/GPL|
7206 Dual\ MPL/GPL|
7207 Proprietary
7208 }x;
7209 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7210 WARN("MODULE_LICENSE",
7211 "unknown module license " . $extracted_string . "\n" . $herecurr);
7212 }
7213 }
6a8d76cb
MC
7214
7215# check for sysctl duplicate constants
7216 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7217 WARN("DUPLICATED_SYSCTL_CONST",
7218 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7219 }
13214adf
AW
7220 }
7221
7222 # If we have no input at all, then there is nothing to report on
7223 # so just keep quiet.
7224 if ($#rawlines == -1) {
7225 exit(0);
0a920b5b
AW
7226 }
7227
8905a67c
AW
7228 # In mailback mode only produce a report in the negative, for
7229 # things that appear to be patches.
7230 if ($mailback && ($clean == 1 || !$is_patch)) {
7231 exit(0);
7232 }
7233
e73d2715 7234 # This is not a patch, and we are in 'no-patch' mode so
8905a67c
AW
7235 # just keep quiet.
7236 if (!$chk_patch && !$is_patch) {
7237 exit(0);
7238 }
7239
a08ffbef 7240 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
000d1cc1
JP
7241 ERROR("NOT_UNIFIED_DIFF",
7242 "Does not appear to be a unified-diff format patch\n");
0a920b5b 7243 }
cd261496
GU
7244 if ($is_patch && $has_commit_log && $chk_signoff) {
7245 if ($signoff == 0) {
7246 ERROR("MISSING_SIGN_OFF",
7247 "Missing Signed-off-by: line(s)\n");
48ca2d8a
DR
7248 } elsif ($authorsignoff != 1) {
7249 # authorsignoff values:
7250 # 0 -> missing sign off
7251 # 1 -> sign off identical
7252 # 2 -> names and addresses match, comments mismatch
7253 # 3 -> addresses match, names different
7254 # 4 -> names match, addresses different
7255 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7256
7257 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7258
7259 if ($authorsignoff == 0) {
7260 ERROR("NO_AUTHOR_SIGN_OFF",
7261 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7262 } elsif ($authorsignoff == 2) {
7263 CHK("FROM_SIGN_OFF_MISMATCH",
7264 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7265 } elsif ($authorsignoff == 3) {
7266 WARN("FROM_SIGN_OFF_MISMATCH",
7267 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7268 } elsif ($authorsignoff == 4) {
7269 WARN("FROM_SIGN_OFF_MISMATCH",
7270 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7271 } elsif ($authorsignoff == 5) {
7272 WARN("FROM_SIGN_OFF_MISMATCH",
7273 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7274 }
cd261496 7275 }
0a920b5b
AW
7276 }
7277
8905a67c 7278 print report_dump();
13214adf
AW
7279 if ($summary && !($clean == 1 && $quiet == 1)) {
7280 print "$filename " if ($summary_file);
8905a67c
AW
7281 print "total: $cnt_error errors, $cnt_warn warnings, " .
7282 (($check)? "$cnt_chk checks, " : "") .
7283 "$cnt_lines lines checked\n";
f0a594c1 7284 }
8905a67c 7285
d2c0a235 7286 if ($quiet == 0) {
ef212196
JP
7287 # If there were any defects found and not already fixing them
7288 if (!$clean and !$fix) {
7289 print << "EOM"
7290
7291NOTE: For some of the reported defects, checkpatch may be able to
7292 mechanically convert to the typical style using --fix or --fix-inplace.
7293EOM
7294 }
d2c0a235
AW
7295 # If there were whitespace errors which cleanpatch can fix
7296 # then suggest that.
7297 if ($rpt_cleaners) {
b0781216 7298 $rpt_cleaners = 0;
d8469f16
JP
7299 print << "EOM"
7300
7301NOTE: Whitespace errors detected.
7302 You may wish to use scripts/cleanpatch or scripts/cleanfile
7303EOM
d2c0a235
AW
7304 }
7305 }
7306
d752fcc8
JP
7307 if ($clean == 0 && $fix &&
7308 ("@rawlines" ne "@fixed" ||
7309 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
9624b8d6
JP
7310 my $newfile = $filename;
7311 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
3705ce5b
JP
7312 my $linecount = 0;
7313 my $f;
7314
d752fcc8
JP
7315 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7316
3705ce5b
JP
7317 open($f, '>', $newfile)
7318 or die "$P: Can't open $newfile for write\n";
7319 foreach my $fixed_line (@fixed) {
7320 $linecount++;
7321 if ($file) {
7322 if ($linecount > 3) {
7323 $fixed_line =~ s/^\+//;
d752fcc8 7324 print $f $fixed_line . "\n";
3705ce5b
JP
7325 }
7326 } else {
7327 print $f $fixed_line . "\n";
7328 }
7329 }
7330 close($f);
7331
7332 if (!$quiet) {
7333 print << "EOM";
d8469f16 7334
3705ce5b
JP
7335Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7336
7337Do _NOT_ trust the results written to this file.
7338Do _NOT_ submit these changes without inspecting them for correctness.
7339
7340This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7341No warranties, expressed or implied...
3705ce5b
JP
7342EOM
7343 }
7344 }
7345
d8469f16
JP
7346 if ($quiet == 0) {
7347 print "\n";
7348 if ($clean == 1) {
7349 print "$vname has no obvious style problems and is ready for submission.\n";
7350 } else {
7351 print "$vname has style problems, please review.\n";
7352 }
0a920b5b
AW
7353 }
7354 return $clean;
7355}