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