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