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