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