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