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