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