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