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