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