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