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