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