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