]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/checkpatch.pl
checkpatch: correctly track the end of preprocessor commands in context
[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;
9
10my $P = $0;
00df344f 11$P =~ s@.*/@@g;
0a920b5b 12
000d1cc1 13my $V = '0.32';
0a920b5b
AW
14
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
773647a0 21my $tst_only;
6c72ffaa 22my $emacs = 0;
8905a67c 23my $terse = 0;
6c72ffaa
AW
24my $file = 0;
25my $check = 0;
8905a67c
AW
26my $summary = 1;
27my $mailback = 0;
13214adf 28my $summary_file = 0;
000d1cc1 29my $show_types = 0;
6c72ffaa 30my $root;
c2fdda0d 31my %debug;
000d1cc1
JP
32my %ignore_type = ();
33my @ignore = ();
77f5b10a 34my $help = 0;
000d1cc1 35my $configuration_file = ".checkpatch.conf";
77f5b10a
HE
36
37sub help {
38 my ($exitcode) = @_;
39
40 print << "EOM";
41Usage: $P [OPTION]... [FILE]...
42Version: $V
43
44Options:
45 -q, --quiet quiet
46 --no-tree run without a kernel tree
47 --no-signoff do not check for 'Signed-off-by' line
48 --patch treat FILE as patchfile (default)
49 --emacs emacs compile window format
50 --terse one line per report
51 -f, --file treat FILE as regular source file
52 --subjective, --strict enable more subjective tests
000d1cc1
JP
53 --ignore TYPE(,TYPE2...) ignore various comma separated message types
54 --show-types show the message "types" in the output
77f5b10a
HE
55 --root=PATH PATH to the kernel tree root
56 --no-summary suppress the per-file summary
57 --mailback only produce a report in case of warnings/errors
58 --summary-file include the filename in summary
59 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
60 'values', 'possible', 'type', and 'attr' (default
61 is all off)
62 --test-only=WORD report only warnings/errors containing WORD
63 literally
64 -h, --help, --version display this help and exit
65
66When FILE is - read standard input.
67EOM
68
69 exit($exitcode);
70}
71
000d1cc1
JP
72my $conf = which_conf($configuration_file);
73if (-f $conf) {
74 my @conf_args;
75 open(my $conffile, '<', "$conf")
76 or warn "$P: Can't find a readable $configuration_file file $!\n";
77
78 while (<$conffile>) {
79 my $line = $_;
80
81 $line =~ s/\s*\n?$//g;
82 $line =~ s/^\s*//g;
83 $line =~ s/\s+/ /g;
84
85 next if ($line =~ m/^\s*#/);
86 next if ($line =~ m/^\s*$/);
87
88 my @words = split(" ", $line);
89 foreach my $word (@words) {
90 last if ($word =~ m/^#/);
91 push (@conf_args, $word);
92 }
93 }
94 close($conffile);
95 unshift(@ARGV, @conf_args) if @conf_args;
96}
97
0a920b5b 98GetOptions(
6c72ffaa 99 'q|quiet+' => \$quiet,
0a920b5b
AW
100 'tree!' => \$tree,
101 'signoff!' => \$chk_signoff,
102 'patch!' => \$chk_patch,
6c72ffaa 103 'emacs!' => \$emacs,
8905a67c 104 'terse!' => \$terse,
77f5b10a 105 'f|file!' => \$file,
6c72ffaa
AW
106 'subjective!' => \$check,
107 'strict!' => \$check,
000d1cc1
JP
108 'ignore=s' => \@ignore,
109 'show-types!' => \$show_types,
6c72ffaa 110 'root=s' => \$root,
8905a67c
AW
111 'summary!' => \$summary,
112 'mailback!' => \$mailback,
13214adf
AW
113 'summary-file!' => \$summary_file,
114
c2fdda0d 115 'debug=s' => \%debug,
773647a0 116 'test-only=s' => \$tst_only,
77f5b10a
HE
117 'h|help' => \$help,
118 'version' => \$help
119) or help(1);
120
121help(0) if ($help);
0a920b5b
AW
122
123my $exit = 0;
124
125if ($#ARGV < 0) {
77f5b10a 126 print "$P: no input files\n";
0a920b5b
AW
127 exit(1);
128}
129
000d1cc1
JP
130@ignore = split(/,/, join(',',@ignore));
131foreach my $word (@ignore) {
132 $word =~ s/\s*\n?$//g;
133 $word =~ s/^\s*//g;
134 $word =~ s/\s+/ /g;
135 $word =~ tr/[a-z]/[A-Z]/;
136
137 next if ($word =~ m/^\s*#/);
138 next if ($word =~ m/^\s*$/);
139
140 $ignore_type{$word}++;
141}
142
c2fdda0d
AW
143my $dbg_values = 0;
144my $dbg_possible = 0;
7429c690 145my $dbg_type = 0;
a1ef277e 146my $dbg_attr = 0;
c2fdda0d 147for my $key (keys %debug) {
21caa13c
AW
148 ## no critic
149 eval "\${dbg_$key} = '$debug{$key}';";
150 die "$@" if ($@);
c2fdda0d
AW
151}
152
d2c0a235
AW
153my $rpt_cleaners = 0;
154
8905a67c
AW
155if ($terse) {
156 $emacs = 1;
157 $quiet++;
158}
159
6c72ffaa
AW
160if ($tree) {
161 if (defined $root) {
162 if (!top_of_kernel_tree($root)) {
163 die "$P: $root: --root does not point at a valid tree\n";
164 }
165 } else {
166 if (top_of_kernel_tree('.')) {
167 $root = '.';
168 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
169 top_of_kernel_tree($1)) {
170 $root = $1;
171 }
172 }
173
174 if (!defined $root) {
175 print "Must be run from the top-level dir. of a kernel tree\n";
176 exit(2);
177 }
0a920b5b
AW
178}
179
6c72ffaa
AW
180my $emitted_corrupt = 0;
181
2ceb532b
AW
182our $Ident = qr{
183 [A-Za-z_][A-Za-z\d_]*
184 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
185 }x;
6c72ffaa
AW
186our $Storage = qr{extern|static|asmlinkage};
187our $Sparse = qr{
188 __user|
189 __kernel|
190 __force|
191 __iomem|
192 __must_check|
193 __init_refok|
417495ed 194 __kprobes|
165e72a6
SE
195 __ref|
196 __rcu
6c72ffaa 197 }x;
52131292
WS
198
199# Notes to $Attribute:
200# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
6c72ffaa
AW
201our $Attribute = qr{
202 const|
03f1df7d
JP
203 __percpu|
204 __nocast|
205 __safe|
206 __bitwise__|
207 __packed__|
208 __packed2__|
209 __naked|
210 __maybe_unused|
211 __always_unused|
212 __noreturn|
213 __used|
214 __cold|
215 __noclone|
216 __deprecated|
6c72ffaa
AW
217 __read_mostly|
218 __kprobes|
52131292 219 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
24e1d81a
AW
220 ____cacheline_aligned|
221 ____cacheline_aligned_in_smp|
5fe3af11
AW
222 ____cacheline_internodealigned_in_smp|
223 __weak
6c72ffaa 224 }x;
c45dcabd 225our $Modifier;
6c72ffaa 226our $Inline = qr{inline|__always_inline|noinline};
6c72ffaa
AW
227our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
228our $Lval = qr{$Ident(?:$Member)*};
229
230our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
231our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
86f9d059 232our $Compare = qr{<=|>=|==|!=|<|>};
6c72ffaa
AW
233our $Operators = qr{
234 <=|>=|==|!=|
235 =>|->|<<|>>|<|>|!|~|
c2fdda0d 236 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
6c72ffaa
AW
237 }x;
238
8905a67c
AW
239our $NonptrType;
240our $Type;
241our $Declare;
242
15662b3e
JP
243our $NON_ASCII_UTF8 = qr{
244 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
171ae1a4
AW
245 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
246 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
247 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
248 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
249 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
250 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
251}x;
252
15662b3e
JP
253our $UTF8 = qr{
254 [\x09\x0A\x0D\x20-\x7E] # ASCII
255 | $NON_ASCII_UTF8
256}x;
257
8ed22cad 258our $typeTypedefs = qr{(?x:
fb9e9096 259 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
8ed22cad
AW
260 atomic_t
261)};
262
691e669b 263our $logFunctions = qr{(?x:
6e60c02e
JP
264 printk(?:_ratelimited|_once|)|
265 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
266 WARN(?:_RATELIMIT|_ONCE|)|
b0531722
JP
267 panic|
268 MODULE_[A-Z_]+
691e669b
JP
269)};
270
20112475
JP
271our $signature_tags = qr{(?xi:
272 Signed-off-by:|
273 Acked-by:|
274 Tested-by:|
275 Reviewed-by:|
276 Reported-by:|
277 To:|
278 Cc:
279)};
280
8905a67c
AW
281our @typeList = (
282 qr{void},
c45dcabd
AW
283 qr{(?:unsigned\s+)?char},
284 qr{(?:unsigned\s+)?short},
285 qr{(?:unsigned\s+)?int},
286 qr{(?:unsigned\s+)?long},
287 qr{(?:unsigned\s+)?long\s+int},
288 qr{(?:unsigned\s+)?long\s+long},
289 qr{(?:unsigned\s+)?long\s+long\s+int},
8905a67c
AW
290 qr{unsigned},
291 qr{float},
292 qr{double},
293 qr{bool},
8905a67c
AW
294 qr{struct\s+$Ident},
295 qr{union\s+$Ident},
296 qr{enum\s+$Ident},
297 qr{${Ident}_t},
298 qr{${Ident}_handler},
299 qr{${Ident}_handler_fn},
300);
c45dcabd
AW
301our @modifierList = (
302 qr{fastcall},
303);
8905a67c 304
7840a94c
WS
305our $allowed_asm_includes = qr{(?x:
306 irq|
307 memory
308)};
309# memory.h: ARM has a custom one
310
8905a67c 311sub build_types {
d2172eb5
AW
312 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
313 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
c8cb2ca3 314 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
8905a67c 315 $NonptrType = qr{
d2172eb5 316 (?:$Modifier\s+|const\s+)*
cf655043 317 (?:
c45dcabd 318 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
8ed22cad 319 (?:$typeTypedefs\b)|
c45dcabd 320 (?:${all}\b)
cf655043 321 )
c8cb2ca3 322 (?:\s+$Modifier|\s+const)*
8905a67c
AW
323 }x;
324 $Type = qr{
c45dcabd 325 $NonptrType
65863862 326 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
c8cb2ca3 327 (?:\s+$Inline|\s+$Modifier)*
8905a67c
AW
328 }x;
329 $Declare = qr{(?:$Storage\s+)?$Type};
330}
331build_types();
6c72ffaa 332
7d2367af
JP
333our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
334
335our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
336our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
337
338sub deparenthesize {
339 my ($string) = @_;
340 return "" if (!defined($string));
341 $string =~ s@^\s*\(\s*@@g;
342 $string =~ s@\s*\)\s*$@@g;
343 $string =~ s@\s+@ @g;
344 return $string;
345}
346
6c72ffaa
AW
347$chk_signoff = 0 if ($file);
348
4a0df2ef
AW
349my @dep_includes = ();
350my @dep_functions = ();
6c72ffaa
AW
351my $removal = "Documentation/feature-removal-schedule.txt";
352if ($tree && -f "$root/$removal") {
21caa13c 353 open(my $REMOVE, '<', "$root/$removal") ||
6c72ffaa 354 die "$P: $removal: open failed - $!\n";
21caa13c 355 while (<$REMOVE>) {
f0a594c1
AW
356 if (/^Check:\s+(.*\S)/) {
357 for my $entry (split(/[, ]+/, $1)) {
358 if ($entry =~ m@include/(.*)@) {
4a0df2ef 359 push(@dep_includes, $1);
4a0df2ef 360
f0a594c1
AW
361 } elsif ($entry !~ m@/@) {
362 push(@dep_functions, $entry);
363 }
4a0df2ef 364 }
0a920b5b
AW
365 }
366 }
21caa13c 367 close($REMOVE);
0a920b5b
AW
368}
369
00df344f 370my @rawlines = ();
c2fdda0d
AW
371my @lines = ();
372my $vname;
6c72ffaa 373for my $filename (@ARGV) {
21caa13c 374 my $FILE;
6c72ffaa 375 if ($file) {
21caa13c 376 open($FILE, '-|', "diff -u /dev/null $filename") ||
6c72ffaa 377 die "$P: $filename: diff failed - $!\n";
21caa13c
AW
378 } elsif ($filename eq '-') {
379 open($FILE, '<&STDIN');
6c72ffaa 380 } else {
21caa13c 381 open($FILE, '<', "$filename") ||
6c72ffaa 382 die "$P: $filename: open failed - $!\n";
0a920b5b 383 }
c2fdda0d
AW
384 if ($filename eq '-') {
385 $vname = 'Your patch';
386 } else {
387 $vname = $filename;
388 }
21caa13c 389 while (<$FILE>) {
6c72ffaa
AW
390 chomp;
391 push(@rawlines, $_);
392 }
21caa13c 393 close($FILE);
c2fdda0d 394 if (!process($filename)) {
6c72ffaa
AW
395 $exit = 1;
396 }
397 @rawlines = ();
13214adf 398 @lines = ();
0a920b5b
AW
399}
400
401exit($exit);
402
403sub top_of_kernel_tree {
6c72ffaa
AW
404 my ($root) = @_;
405
406 my @tree_check = (
407 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
408 "README", "Documentation", "arch", "include", "drivers",
409 "fs", "init", "ipc", "kernel", "lib", "scripts",
410 );
411
412 foreach my $check (@tree_check) {
413 if (! -e $root . '/' . $check) {
414 return 0;
415 }
0a920b5b 416 }
6c72ffaa 417 return 1;
000d1cc1 418 }
0a920b5b 419
20112475
JP
420sub parse_email {
421 my ($formatted_email) = @_;
422
423 my $name = "";
424 my $address = "";
425 my $comment = "";
426
427 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
428 $name = $1;
429 $address = $2;
430 $comment = $3 if defined $3;
431 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
432 $address = $1;
433 $comment = $2 if defined $2;
434 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
435 $address = $1;
436 $comment = $2 if defined $2;
437 $formatted_email =~ s/$address.*$//;
438 $name = $formatted_email;
439 $name =~ s/^\s+|\s+$//g;
440 $name =~ s/^\"|\"$//g;
441 # If there's a name left after stripping spaces and
442 # leading quotes, and the address doesn't have both
443 # leading and trailing angle brackets, the address
444 # is invalid. ie:
445 # "joe smith joe@smith.com" bad
446 # "joe smith <joe@smith.com" bad
447 if ($name ne "" && $address !~ /^<[^>]+>$/) {
448 $name = "";
449 $address = "";
450 $comment = "";
451 }
452 }
453
454 $name =~ s/^\s+|\s+$//g;
455 $name =~ s/^\"|\"$//g;
456 $address =~ s/^\s+|\s+$//g;
457 $address =~ s/^\<|\>$//g;
458
459 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
460 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
461 $name = "\"$name\"";
462 }
463
464 return ($name, $address, $comment);
465}
466
467sub format_email {
468 my ($name, $address) = @_;
469
470 my $formatted_email;
471
472 $name =~ s/^\s+|\s+$//g;
473 $name =~ s/^\"|\"$//g;
474 $address =~ s/^\s+|\s+$//g;
475
476 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
477 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
478 $name = "\"$name\"";
479 }
480
481 if ("$name" eq "") {
482 $formatted_email = "$address";
483 } else {
484 $formatted_email = "$name <$address>";
485 }
486
487 return $formatted_email;
488}
489
000d1cc1
JP
490sub which_conf {
491 my ($conf) = @_;
492
493 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
494 if (-e "$path/$conf") {
495 return "$path/$conf";
496 }
497 }
498
499 return "";
500}
501
0a920b5b
AW
502sub expand_tabs {
503 my ($str) = @_;
504
505 my $res = '';
506 my $n = 0;
507 for my $c (split(//, $str)) {
508 if ($c eq "\t") {
509 $res .= ' ';
510 $n++;
511 for (; ($n % 8) != 0; $n++) {
512 $res .= ' ';
513 }
514 next;
515 }
516 $res .= $c;
517 $n++;
518 }
519
520 return $res;
521}
6c72ffaa 522sub copy_spacing {
773647a0 523 (my $res = shift) =~ tr/\t/ /c;
6c72ffaa
AW
524 return $res;
525}
0a920b5b 526
4a0df2ef
AW
527sub line_stats {
528 my ($line) = @_;
529
530 # Drop the diff line leader and expand tabs
531 $line =~ s/^.//;
532 $line = expand_tabs($line);
533
534 # Pick the indent from the front of the line.
535 my ($white) = ($line =~ /^(\s*)/);
536
537 return (length($line), length($white));
538}
539
773647a0
AW
540my $sanitise_quote = '';
541
542sub sanitise_line_reset {
543 my ($in_comment) = @_;
544
545 if ($in_comment) {
546 $sanitise_quote = '*/';
547 } else {
548 $sanitise_quote = '';
549 }
550}
00df344f
AW
551sub sanitise_line {
552 my ($line) = @_;
553
554 my $res = '';
555 my $l = '';
556
c2fdda0d 557 my $qlen = 0;
773647a0
AW
558 my $off = 0;
559 my $c;
00df344f 560
773647a0
AW
561 # Always copy over the diff marker.
562 $res = substr($line, 0, 1);
563
564 for ($off = 1; $off < length($line); $off++) {
565 $c = substr($line, $off, 1);
566
567 # Comments we are wacking completly including the begin
568 # and end, all to $;.
569 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
570 $sanitise_quote = '*/';
571
572 substr($res, $off, 2, "$;$;");
573 $off++;
574 next;
00df344f 575 }
81bc0e02 576 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
773647a0
AW
577 $sanitise_quote = '';
578 substr($res, $off, 2, "$;$;");
579 $off++;
580 next;
c2fdda0d 581 }
113f04a8
DW
582 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
583 $sanitise_quote = '//';
584
585 substr($res, $off, 2, $sanitise_quote);
586 $off++;
587 next;
588 }
773647a0
AW
589
590 # A \ in a string means ignore the next character.
591 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
592 $c eq "\\") {
593 substr($res, $off, 2, 'XX');
594 $off++;
595 next;
00df344f 596 }
773647a0
AW
597 # Regular quotes.
598 if ($c eq "'" || $c eq '"') {
599 if ($sanitise_quote eq '') {
600 $sanitise_quote = $c;
00df344f 601
773647a0
AW
602 substr($res, $off, 1, $c);
603 next;
604 } elsif ($sanitise_quote eq $c) {
605 $sanitise_quote = '';
606 }
607 }
00df344f 608
fae17dae 609 #print "c<$c> SQ<$sanitise_quote>\n";
773647a0
AW
610 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
611 substr($res, $off, 1, $;);
113f04a8
DW
612 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
613 substr($res, $off, 1, $;);
773647a0
AW
614 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
615 substr($res, $off, 1, 'X');
616 } else {
617 substr($res, $off, 1, $c);
618 }
c2fdda0d
AW
619 }
620
113f04a8
DW
621 if ($sanitise_quote eq '//') {
622 $sanitise_quote = '';
623 }
624
c2fdda0d 625 # The pathname on a #include may be surrounded by '<' and '>'.
c45dcabd 626 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
c2fdda0d
AW
627 my $clean = 'X' x length($1);
628 $res =~ s@\<.*\>@<$clean>@;
629
630 # The whole of a #error is a string.
c45dcabd 631 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
c2fdda0d 632 my $clean = 'X' x length($1);
c45dcabd 633 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
c2fdda0d
AW
634 }
635
00df344f
AW
636 return $res;
637}
638
8905a67c
AW
639sub ctx_statement_block {
640 my ($linenr, $remain, $off) = @_;
641 my $line = $linenr - 1;
642 my $blk = '';
643 my $soff = $off;
644 my $coff = $off - 1;
773647a0 645 my $coff_set = 0;
8905a67c 646
13214adf
AW
647 my $loff = 0;
648
8905a67c
AW
649 my $type = '';
650 my $level = 0;
a2750645 651 my @stack = ();
cf655043 652 my $p;
8905a67c
AW
653 my $c;
654 my $len = 0;
13214adf
AW
655
656 my $remainder;
8905a67c 657 while (1) {
a2750645
AW
658 @stack = (['', 0]) if ($#stack == -1);
659
773647a0 660 #warn "CSB: blk<$blk> remain<$remain>\n";
8905a67c
AW
661 # If we are about to drop off the end, pull in more
662 # context.
663 if ($off >= $len) {
664 for (; $remain > 0; $line++) {
dea33496 665 last if (!defined $lines[$line]);
c2fdda0d 666 next if ($lines[$line] =~ /^-/);
8905a67c 667 $remain--;
13214adf 668 $loff = $len;
c2fdda0d 669 $blk .= $lines[$line] . "\n";
8905a67c
AW
670 $len = length($blk);
671 $line++;
672 last;
673 }
674 # Bail if there is no further context.
675 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
13214adf 676 if ($off >= $len) {
8905a67c
AW
677 last;
678 }
f74bd194
AW
679 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
680 $level++;
681 $type = '#';
682 }
8905a67c 683 }
cf655043 684 $p = $c;
8905a67c 685 $c = substr($blk, $off, 1);
13214adf 686 $remainder = substr($blk, $off);
8905a67c 687
773647a0 688 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4635f4fb
AW
689
690 # Handle nested #if/#else.
691 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
692 push(@stack, [ $type, $level ]);
693 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
694 ($type, $level) = @{$stack[$#stack - 1]};
695 } elsif ($remainder =~ /^#\s*endif\b/) {
696 ($type, $level) = @{pop(@stack)};
697 }
698
8905a67c
AW
699 # Statement ends at the ';' or a close '}' at the
700 # outermost level.
701 if ($level == 0 && $c eq ';') {
702 last;
703 }
704
13214adf 705 # An else is really a conditional as long as its not else if
773647a0
AW
706 if ($level == 0 && $coff_set == 0 &&
707 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
708 $remainder =~ /^(else)(?:\s|{)/ &&
709 $remainder !~ /^else\s+if\b/) {
710 $coff = $off + length($1) - 1;
711 $coff_set = 1;
712 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
713 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
13214adf
AW
714 }
715
8905a67c
AW
716 if (($type eq '' || $type eq '(') && $c eq '(') {
717 $level++;
718 $type = '(';
719 }
720 if ($type eq '(' && $c eq ')') {
721 $level--;
722 $type = ($level != 0)? '(' : '';
723
724 if ($level == 0 && $coff < $soff) {
725 $coff = $off;
773647a0
AW
726 $coff_set = 1;
727 #warn "CSB: mark coff<$coff>\n";
8905a67c
AW
728 }
729 }
730 if (($type eq '' || $type eq '{') && $c eq '{') {
731 $level++;
732 $type = '{';
733 }
734 if ($type eq '{' && $c eq '}') {
735 $level--;
736 $type = ($level != 0)? '{' : '';
737
738 if ($level == 0) {
b998e001
PP
739 if (substr($blk, $off + 1, 1) eq ';') {
740 $off++;
741 }
8905a67c
AW
742 last;
743 }
744 }
f74bd194
AW
745 # Preprocessor commands end at the newline unless escaped.
746 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
747 $level--;
748 $type = '';
749 $off++;
750 last;
751 }
8905a67c
AW
752 $off++;
753 }
a3bb97a7 754 # We are truly at the end, so shuffle to the next line.
13214adf 755 if ($off == $len) {
a3bb97a7 756 $loff = $len + 1;
13214adf
AW
757 $line++;
758 $remain--;
759 }
8905a67c
AW
760
761 my $statement = substr($blk, $soff, $off - $soff + 1);
762 my $condition = substr($blk, $soff, $coff - $soff + 1);
763
764 #warn "STATEMENT<$statement>\n";
765 #warn "CONDITION<$condition>\n";
766
773647a0 767 #print "coff<$coff> soff<$off> loff<$loff>\n";
13214adf
AW
768
769 return ($statement, $condition,
770 $line, $remain + 1, $off - $loff + 1, $level);
771}
772
cf655043
AW
773sub statement_lines {
774 my ($stmt) = @_;
775
776 # Strip the diff line prefixes and rip blank lines at start and end.
777 $stmt =~ s/(^|\n)./$1/g;
778 $stmt =~ s/^\s*//;
779 $stmt =~ s/\s*$//;
780
781 my @stmt_lines = ($stmt =~ /\n/g);
782
783 return $#stmt_lines + 2;
784}
785
786sub statement_rawlines {
787 my ($stmt) = @_;
788
789 my @stmt_lines = ($stmt =~ /\n/g);
790
791 return $#stmt_lines + 2;
792}
793
794sub statement_block_size {
795 my ($stmt) = @_;
796
797 $stmt =~ s/(^|\n)./$1/g;
798 $stmt =~ s/^\s*{//;
799 $stmt =~ s/}\s*$//;
800 $stmt =~ s/^\s*//;
801 $stmt =~ s/\s*$//;
802
803 my @stmt_lines = ($stmt =~ /\n/g);
804 my @stmt_statements = ($stmt =~ /;/g);
805
806 my $stmt_lines = $#stmt_lines + 2;
807 my $stmt_statements = $#stmt_statements + 1;
808
809 if ($stmt_lines > $stmt_statements) {
810 return $stmt_lines;
811 } else {
812 return $stmt_statements;
813 }
814}
815
13214adf
AW
816sub ctx_statement_full {
817 my ($linenr, $remain, $off) = @_;
818 my ($statement, $condition, $level);
819
820 my (@chunks);
821
cf655043 822 # Grab the first conditional/block pair.
13214adf
AW
823 ($statement, $condition, $linenr, $remain, $off, $level) =
824 ctx_statement_block($linenr, $remain, $off);
773647a0 825 #print "F: c<$condition> s<$statement> remain<$remain>\n";
cf655043
AW
826 push(@chunks, [ $condition, $statement ]);
827 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
828 return ($level, $linenr, @chunks);
829 }
830
831 # Pull in the following conditional/block pairs and see if they
832 # could continue the statement.
13214adf 833 for (;;) {
13214adf
AW
834 ($statement, $condition, $linenr, $remain, $off, $level) =
835 ctx_statement_block($linenr, $remain, $off);
cf655043 836 #print "C: c<$condition> s<$statement> remain<$remain>\n";
773647a0 837 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
cf655043
AW
838 #print "C: push\n";
839 push(@chunks, [ $condition, $statement ]);
13214adf
AW
840 }
841
842 return ($level, $linenr, @chunks);
8905a67c
AW
843}
844
4a0df2ef 845sub ctx_block_get {
f0a594c1 846 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
4a0df2ef
AW
847 my $line;
848 my $start = $linenr - 1;
4a0df2ef
AW
849 my $blk = '';
850 my @o;
851 my @c;
852 my @res = ();
853
f0a594c1 854 my $level = 0;
4635f4fb 855 my @stack = ($level);
00df344f
AW
856 for ($line = $start; $remain > 0; $line++) {
857 next if ($rawlines[$line] =~ /^-/);
858 $remain--;
859
860 $blk .= $rawlines[$line];
4635f4fb
AW
861
862 # Handle nested #if/#else.
01464f30 863 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
4635f4fb 864 push(@stack, $level);
01464f30 865 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
4635f4fb 866 $level = $stack[$#stack - 1];
01464f30 867 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
4635f4fb
AW
868 $level = pop(@stack);
869 }
870
01464f30 871 foreach my $c (split(//, $lines[$line])) {
f0a594c1
AW
872 ##print "C<$c>L<$level><$open$close>O<$off>\n";
873 if ($off > 0) {
874 $off--;
875 next;
876 }
4a0df2ef 877
f0a594c1
AW
878 if ($c eq $close && $level > 0) {
879 $level--;
880 last if ($level == 0);
881 } elsif ($c eq $open) {
882 $level++;
883 }
884 }
4a0df2ef 885
f0a594c1 886 if (!$outer || $level <= 1) {
00df344f 887 push(@res, $rawlines[$line]);
4a0df2ef
AW
888 }
889
f0a594c1 890 last if ($level == 0);
4a0df2ef
AW
891 }
892
f0a594c1 893 return ($level, @res);
4a0df2ef
AW
894}
895sub ctx_block_outer {
896 my ($linenr, $remain) = @_;
897
f0a594c1
AW
898 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
899 return @r;
4a0df2ef
AW
900}
901sub ctx_block {
902 my ($linenr, $remain) = @_;
903
f0a594c1
AW
904 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
905 return @r;
653d4876
AW
906}
907sub ctx_statement {
f0a594c1
AW
908 my ($linenr, $remain, $off) = @_;
909
910 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
911 return @r;
912}
913sub ctx_block_level {
653d4876
AW
914 my ($linenr, $remain) = @_;
915
f0a594c1 916 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
4a0df2ef 917}
9c0ca6f9
AW
918sub ctx_statement_level {
919 my ($linenr, $remain, $off) = @_;
920
921 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
922}
4a0df2ef
AW
923
924sub ctx_locate_comment {
925 my ($first_line, $end_line) = @_;
926
927 # Catch a comment on the end of the line itself.
beae6332 928 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
4a0df2ef
AW
929 return $current_comment if (defined $current_comment);
930
931 # Look through the context and try and figure out if there is a
932 # comment.
933 my $in_comment = 0;
934 $current_comment = '';
935 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
00df344f
AW
936 my $line = $rawlines[$linenr - 1];
937 #warn " $line\n";
4a0df2ef
AW
938 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
939 $in_comment = 1;
940 }
941 if ($line =~ m@/\*@) {
942 $in_comment = 1;
943 }
944 if (!$in_comment && $current_comment ne '') {
945 $current_comment = '';
946 }
947 $current_comment .= $line . "\n" if ($in_comment);
948 if ($line =~ m@\*/@) {
949 $in_comment = 0;
950 }
951 }
952
953 chomp($current_comment);
954 return($current_comment);
955}
956sub ctx_has_comment {
957 my ($first_line, $end_line) = @_;
958 my $cmt = ctx_locate_comment($first_line, $end_line);
959
00df344f 960 ##print "LINE: $rawlines[$end_line - 1 ]\n";
4a0df2ef
AW
961 ##print "CMMT: $cmt\n";
962
963 return ($cmt ne '');
964}
965
4d001e4d
AW
966sub raw_line {
967 my ($linenr, $cnt) = @_;
968
969 my $offset = $linenr - 1;
970 $cnt++;
971
972 my $line;
973 while ($cnt) {
974 $line = $rawlines[$offset++];
975 next if (defined($line) && $line =~ /^-/);
976 $cnt--;
977 }
978
979 return $line;
980}
981
6c72ffaa
AW
982sub cat_vet {
983 my ($vet) = @_;
984 my ($res, $coded);
9c0ca6f9 985
6c72ffaa
AW
986 $res = '';
987 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
988 $res .= $1;
989 if ($2 ne '') {
990 $coded = sprintf("^%c", unpack('C', $2) + 64);
991 $res .= $coded;
9c0ca6f9
AW
992 }
993 }
6c72ffaa 994 $res =~ s/$/\$/;
9c0ca6f9 995
6c72ffaa 996 return $res;
9c0ca6f9
AW
997}
998
c2fdda0d 999my $av_preprocessor = 0;
cf655043 1000my $av_pending;
c2fdda0d 1001my @av_paren_type;
1f65f947 1002my $av_pend_colon;
c2fdda0d
AW
1003
1004sub annotate_reset {
1005 $av_preprocessor = 0;
cf655043
AW
1006 $av_pending = '_';
1007 @av_paren_type = ('E');
1f65f947 1008 $av_pend_colon = 'O';
c2fdda0d
AW
1009}
1010
6c72ffaa
AW
1011sub annotate_values {
1012 my ($stream, $type) = @_;
0a920b5b 1013
6c72ffaa 1014 my $res;
1f65f947 1015 my $var = '_' x length($stream);
6c72ffaa
AW
1016 my $cur = $stream;
1017
c2fdda0d 1018 print "$stream\n" if ($dbg_values > 1);
6c72ffaa 1019
6c72ffaa 1020 while (length($cur)) {
773647a0 1021 @av_paren_type = ('E') if ($#av_paren_type < 0);
cf655043 1022 print " <" . join('', @av_paren_type) .
171ae1a4 1023 "> <$type> <$av_pending>" if ($dbg_values > 1);
6c72ffaa 1024 if ($cur =~ /^(\s+)/o) {
c2fdda0d
AW
1025 print "WS($1)\n" if ($dbg_values > 1);
1026 if ($1 =~ /\n/ && $av_preprocessor) {
cf655043 1027 $type = pop(@av_paren_type);
c2fdda0d 1028 $av_preprocessor = 0;
6c72ffaa
AW
1029 }
1030
c023e473 1031 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
9446ef56
AW
1032 print "CAST($1)\n" if ($dbg_values > 1);
1033 push(@av_paren_type, $type);
1034 $type = 'C';
1035
e91b6e26 1036 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
c2fdda0d 1037 print "DECLARE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1038 $type = 'T';
1039
389a2fe5
AW
1040 } elsif ($cur =~ /^($Modifier)\s*/) {
1041 print "MODIFIER($1)\n" if ($dbg_values > 1);
1042 $type = 'T';
1043
c45dcabd 1044 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
171ae1a4 1045 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
c2fdda0d 1046 $av_preprocessor = 1;
171ae1a4
AW
1047 push(@av_paren_type, $type);
1048 if ($2 ne '') {
1049 $av_pending = 'N';
1050 }
1051 $type = 'E';
1052
c45dcabd 1053 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
171ae1a4
AW
1054 print "UNDEF($1)\n" if ($dbg_values > 1);
1055 $av_preprocessor = 1;
1056 push(@av_paren_type, $type);
6c72ffaa 1057
c45dcabd 1058 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
cf655043 1059 print "PRE_START($1)\n" if ($dbg_values > 1);
c2fdda0d 1060 $av_preprocessor = 1;
cf655043
AW
1061
1062 push(@av_paren_type, $type);
1063 push(@av_paren_type, $type);
171ae1a4 1064 $type = 'E';
cf655043 1065
c45dcabd 1066 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
cf655043
AW
1067 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1068 $av_preprocessor = 1;
1069
1070 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1071
171ae1a4 1072 $type = 'E';
cf655043 1073
c45dcabd 1074 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
cf655043
AW
1075 print "PRE_END($1)\n" if ($dbg_values > 1);
1076
1077 $av_preprocessor = 1;
1078
1079 # Assume all arms of the conditional end as this
1080 # one does, and continue as if the #endif was not here.
1081 pop(@av_paren_type);
1082 push(@av_paren_type, $type);
171ae1a4 1083 $type = 'E';
6c72ffaa
AW
1084
1085 } elsif ($cur =~ /^(\\\n)/o) {
c2fdda0d 1086 print "PRECONT($1)\n" if ($dbg_values > 1);
6c72ffaa 1087
171ae1a4
AW
1088 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1089 print "ATTR($1)\n" if ($dbg_values > 1);
1090 $av_pending = $type;
1091 $type = 'N';
1092
6c72ffaa 1093 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
c2fdda0d 1094 print "SIZEOF($1)\n" if ($dbg_values > 1);
6c72ffaa 1095 if (defined $2) {
cf655043 1096 $av_pending = 'V';
6c72ffaa
AW
1097 }
1098 $type = 'N';
1099
14b111c1 1100 } elsif ($cur =~ /^(if|while|for)\b/o) {
c2fdda0d 1101 print "COND($1)\n" if ($dbg_values > 1);
14b111c1 1102 $av_pending = 'E';
6c72ffaa
AW
1103 $type = 'N';
1104
1f65f947
AW
1105 } elsif ($cur =~/^(case)/o) {
1106 print "CASE($1)\n" if ($dbg_values > 1);
1107 $av_pend_colon = 'C';
1108 $type = 'N';
1109
14b111c1 1110 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
c2fdda0d 1111 print "KEYWORD($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1112 $type = 'N';
1113
1114 } elsif ($cur =~ /^(\()/o) {
c2fdda0d 1115 print "PAREN('$1')\n" if ($dbg_values > 1);
cf655043
AW
1116 push(@av_paren_type, $av_pending);
1117 $av_pending = '_';
6c72ffaa
AW
1118 $type = 'N';
1119
1120 } elsif ($cur =~ /^(\))/o) {
cf655043
AW
1121 my $new_type = pop(@av_paren_type);
1122 if ($new_type ne '_') {
1123 $type = $new_type;
c2fdda0d
AW
1124 print "PAREN('$1') -> $type\n"
1125 if ($dbg_values > 1);
6c72ffaa 1126 } else {
c2fdda0d 1127 print "PAREN('$1')\n" if ($dbg_values > 1);
6c72ffaa
AW
1128 }
1129
c8cb2ca3 1130 } elsif ($cur =~ /^($Ident)\s*\(/o) {
c2fdda0d 1131 print "FUNC($1)\n" if ($dbg_values > 1);
c8cb2ca3 1132 $type = 'V';
cf655043 1133 $av_pending = 'V';
6c72ffaa 1134
8e761b04
AW
1135 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1136 if (defined $2 && $type eq 'C' || $type eq 'T') {
1f65f947 1137 $av_pend_colon = 'B';
8e761b04
AW
1138 } elsif ($type eq 'E') {
1139 $av_pend_colon = 'L';
1f65f947
AW
1140 }
1141 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1142 $type = 'V';
1143
6c72ffaa 1144 } elsif ($cur =~ /^($Ident|$Constant)/o) {
c2fdda0d 1145 print "IDENT($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1146 $type = 'V';
1147
1148 } elsif ($cur =~ /^($Assignment)/o) {
c2fdda0d 1149 print "ASSIGN($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1150 $type = 'N';
1151
cf655043 1152 } elsif ($cur =~/^(;|{|})/) {
c2fdda0d 1153 print "END($1)\n" if ($dbg_values > 1);
13214adf 1154 $type = 'E';
1f65f947
AW
1155 $av_pend_colon = 'O';
1156
8e761b04
AW
1157 } elsif ($cur =~/^(,)/) {
1158 print "COMMA($1)\n" if ($dbg_values > 1);
1159 $type = 'C';
1160
1f65f947
AW
1161 } elsif ($cur =~ /^(\?)/o) {
1162 print "QUESTION($1)\n" if ($dbg_values > 1);
1163 $type = 'N';
1164
1165 } elsif ($cur =~ /^(:)/o) {
1166 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1167
1168 substr($var, length($res), 1, $av_pend_colon);
1169 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1170 $type = 'E';
1171 } else {
1172 $type = 'N';
1173 }
1174 $av_pend_colon = 'O';
13214adf 1175
8e761b04 1176 } elsif ($cur =~ /^(\[)/o) {
13214adf 1177 print "CLOSE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1178 $type = 'N';
1179
0d413866 1180 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
74048ed8
AW
1181 my $variant;
1182
1183 print "OPV($1)\n" if ($dbg_values > 1);
1184 if ($type eq 'V') {
1185 $variant = 'B';
1186 } else {
1187 $variant = 'U';
1188 }
1189
1190 substr($var, length($res), 1, $variant);
1191 $type = 'N';
1192
6c72ffaa 1193 } elsif ($cur =~ /^($Operators)/o) {
c2fdda0d 1194 print "OP($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1195 if ($1 ne '++' && $1 ne '--') {
1196 $type = 'N';
1197 }
1198
1199 } elsif ($cur =~ /(^.)/o) {
c2fdda0d 1200 print "C($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1201 }
1202 if (defined $1) {
1203 $cur = substr($cur, length($1));
1204 $res .= $type x length($1);
1205 }
9c0ca6f9 1206 }
0a920b5b 1207
1f65f947 1208 return ($res, $var);
0a920b5b
AW
1209}
1210
8905a67c 1211sub possible {
13214adf 1212 my ($possible, $line) = @_;
9a974fdb 1213 my $notPermitted = qr{(?:
0776e594
AW
1214 ^(?:
1215 $Modifier|
1216 $Storage|
1217 $Type|
9a974fdb
AW
1218 DEFINE_\S+
1219 )$|
1220 ^(?:
0776e594
AW
1221 goto|
1222 return|
1223 case|
1224 else|
1225 asm|__asm__|
1226 do
9a974fdb 1227 )(?:\s|$)|
0776e594 1228 ^(?:typedef|struct|enum)\b
9a974fdb
AW
1229 )}x;
1230 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1231 if ($possible !~ $notPermitted) {
c45dcabd
AW
1232 # Check for modifiers.
1233 $possible =~ s/\s*$Storage\s*//g;
1234 $possible =~ s/\s*$Sparse\s*//g;
1235 if ($possible =~ /^\s*$/) {
1236
1237 } elsif ($possible =~ /\s/) {
1238 $possible =~ s/\s*$Type\s*//g;
d2506586 1239 for my $modifier (split(' ', $possible)) {
9a974fdb
AW
1240 if ($modifier !~ $notPermitted) {
1241 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1242 push(@modifierList, $modifier);
1243 }
d2506586 1244 }
c45dcabd
AW
1245
1246 } else {
1247 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1248 push(@typeList, $possible);
1249 }
8905a67c 1250 build_types();
0776e594
AW
1251 } else {
1252 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
8905a67c
AW
1253 }
1254}
1255
6c72ffaa
AW
1256my $prefix = '';
1257
000d1cc1
JP
1258sub show_type {
1259 return !defined $ignore_type{$_[0]};
1260}
1261
f0a594c1 1262sub report {
000d1cc1
JP
1263 if (!show_type($_[1]) ||
1264 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
773647a0
AW
1265 return 0;
1266 }
000d1cc1
JP
1267 my $line;
1268 if ($show_types) {
1269 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1270 } else {
1271 $line = "$prefix$_[0]: $_[2]\n";
1272 }
8905a67c
AW
1273 $line = (split('\n', $line))[0] . "\n" if ($terse);
1274
13214adf 1275 push(our @report, $line);
773647a0
AW
1276
1277 return 1;
f0a594c1
AW
1278}
1279sub report_dump {
13214adf 1280 our @report;
f0a594c1 1281}
000d1cc1 1282
de7d4f0e 1283sub ERROR {
000d1cc1 1284 if (report("ERROR", $_[0], $_[1])) {
773647a0
AW
1285 our $clean = 0;
1286 our $cnt_error++;
1287 }
de7d4f0e
AW
1288}
1289sub WARN {
000d1cc1 1290 if (report("WARNING", $_[0], $_[1])) {
773647a0
AW
1291 our $clean = 0;
1292 our $cnt_warn++;
1293 }
de7d4f0e
AW
1294}
1295sub CHK {
000d1cc1 1296 if ($check && report("CHECK", $_[0], $_[1])) {
6c72ffaa
AW
1297 our $clean = 0;
1298 our $cnt_chk++;
1299 }
de7d4f0e
AW
1300}
1301
6ecd9674
AW
1302sub check_absolute_file {
1303 my ($absolute, $herecurr) = @_;
1304 my $file = $absolute;
1305
1306 ##print "absolute<$absolute>\n";
1307
1308 # See if any suffix of this path is a path within the tree.
1309 while ($file =~ s@^[^/]*/@@) {
1310 if (-f "$root/$file") {
1311 ##print "file<$file>\n";
1312 last;
1313 }
1314 }
1315 if (! -f _) {
1316 return 0;
1317 }
1318
1319 # It is, so see if the prefix is acceptable.
1320 my $prefix = $absolute;
1321 substr($prefix, -length($file)) = '';
1322
1323 ##print "prefix<$prefix>\n";
1324 if ($prefix ne ".../") {
000d1cc1
JP
1325 WARN("USE_RELATIVE_PATH",
1326 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
6ecd9674
AW
1327 }
1328}
1329
0a920b5b
AW
1330sub process {
1331 my $filename = shift;
0a920b5b
AW
1332
1333 my $linenr=0;
1334 my $prevline="";
c2fdda0d 1335 my $prevrawline="";
0a920b5b 1336 my $stashline="";
c2fdda0d 1337 my $stashrawline="";
0a920b5b 1338
4a0df2ef 1339 my $length;
0a920b5b
AW
1340 my $indent;
1341 my $previndent=0;
1342 my $stashindent=0;
1343
de7d4f0e 1344 our $clean = 1;
0a920b5b
AW
1345 my $signoff = 0;
1346 my $is_patch = 0;
1347
15662b3e
JP
1348 my $in_header_lines = 1;
1349 my $in_commit_log = 0; #Scanning lines before patch
1350
13214adf 1351 our @report = ();
6c72ffaa
AW
1352 our $cnt_lines = 0;
1353 our $cnt_error = 0;
1354 our $cnt_warn = 0;
1355 our $cnt_chk = 0;
1356
0a920b5b
AW
1357 # Trace the real file/line as we go.
1358 my $realfile = '';
1359 my $realline = 0;
1360 my $realcnt = 0;
1361 my $here = '';
1362 my $in_comment = 0;
c2fdda0d 1363 my $comment_edge = 0;
0a920b5b 1364 my $first_line = 0;
1e855726 1365 my $p1_prefix = '';
0a920b5b 1366
13214adf
AW
1367 my $prev_values = 'E';
1368
1369 # suppression flags
773647a0 1370 my %suppress_ifbraces;
170d3a22 1371 my %suppress_whiletrailers;
2b474a1a 1372 my %suppress_export;
653d4876 1373
c2fdda0d 1374 # Pre-scan the patch sanitizing the lines.
de7d4f0e 1375 # Pre-scan the patch looking for any __setup documentation.
c2fdda0d 1376 #
de7d4f0e
AW
1377 my @setup_docs = ();
1378 my $setup_docs = 0;
773647a0
AW
1379
1380 sanitise_line_reset();
c2fdda0d
AW
1381 my $line;
1382 foreach my $rawline (@rawlines) {
773647a0
AW
1383 $linenr++;
1384 $line = $rawline;
c2fdda0d 1385
773647a0 1386 if ($rawline=~/^\+\+\+\s+(\S+)/) {
de7d4f0e
AW
1387 $setup_docs = 0;
1388 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1389 $setup_docs = 1;
1390 }
773647a0
AW
1391 #next;
1392 }
1393 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1394 $realline=$1-1;
1395 if (defined $2) {
1396 $realcnt=$3+1;
1397 } else {
1398 $realcnt=1+1;
1399 }
c45dcabd 1400 $in_comment = 0;
773647a0
AW
1401
1402 # Guestimate if this is a continuing comment. Run
1403 # the context looking for a comment "edge". If this
1404 # edge is a close comment then we must be in a comment
1405 # at context start.
1406 my $edge;
01fa9147
AW
1407 my $cnt = $realcnt;
1408 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1409 next if (defined $rawlines[$ln - 1] &&
1410 $rawlines[$ln - 1] =~ /^-/);
1411 $cnt--;
1412 #print "RAW<$rawlines[$ln - 1]>\n";
721c1cb6 1413 last if (!defined $rawlines[$ln - 1]);
fae17dae
AW
1414 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1415 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1416 ($edge) = $1;
1417 last;
1418 }
773647a0
AW
1419 }
1420 if (defined $edge && $edge eq '*/') {
1421 $in_comment = 1;
1422 }
1423
1424 # Guestimate if this is a continuing comment. If this
1425 # is the start of a diff block and this line starts
1426 # ' *' then it is very likely a comment.
1427 if (!defined $edge &&
83242e0c 1428 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
773647a0
AW
1429 {
1430 $in_comment = 1;
1431 }
1432
1433 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1434 sanitise_line_reset($in_comment);
1435
171ae1a4 1436 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
773647a0 1437 # Standardise the strings and chars within the input to
171ae1a4 1438 # simplify matching -- only bother with positive lines.
773647a0 1439 $line = sanitise_line($rawline);
de7d4f0e 1440 }
773647a0
AW
1441 push(@lines, $line);
1442
1443 if ($realcnt > 1) {
1444 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1445 } else {
1446 $realcnt = 0;
1447 }
1448
1449 #print "==>$rawline\n";
1450 #print "-->$line\n";
de7d4f0e
AW
1451
1452 if ($setup_docs && $line =~ /^\+/) {
1453 push(@setup_docs, $line);
1454 }
1455 }
1456
6c72ffaa
AW
1457 $prefix = '';
1458
773647a0
AW
1459 $realcnt = 0;
1460 $linenr = 0;
0a920b5b
AW
1461 foreach my $line (@lines) {
1462 $linenr++;
1463
c2fdda0d 1464 my $rawline = $rawlines[$linenr - 1];
6c72ffaa 1465
0a920b5b 1466#extract the line range in the file after the patch is applied
6c72ffaa 1467 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
0a920b5b 1468 $is_patch = 1;
4a0df2ef 1469 $first_line = $linenr + 1;
0a920b5b
AW
1470 $realline=$1-1;
1471 if (defined $2) {
1472 $realcnt=$3+1;
1473 } else {
1474 $realcnt=1+1;
1475 }
c2fdda0d 1476 annotate_reset();
13214adf
AW
1477 $prev_values = 'E';
1478
773647a0 1479 %suppress_ifbraces = ();
170d3a22 1480 %suppress_whiletrailers = ();
2b474a1a 1481 %suppress_export = ();
0a920b5b 1482 next;
0a920b5b 1483
4a0df2ef
AW
1484# track the line number as we move through the hunk, note that
1485# new versions of GNU diff omit the leading space on completely
1486# blank context lines so we need to count that too.
773647a0 1487 } elsif ($line =~ /^( |\+|$)/) {
0a920b5b 1488 $realline++;
d8aaf121 1489 $realcnt-- if ($realcnt != 0);
0a920b5b 1490
4a0df2ef 1491 # Measure the line length and indent.
c2fdda0d 1492 ($length, $indent) = line_stats($rawline);
0a920b5b
AW
1493
1494 # Track the previous line.
1495 ($prevline, $stashline) = ($stashline, $line);
1496 ($previndent, $stashindent) = ($stashindent, $indent);
c2fdda0d
AW
1497 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1498
773647a0 1499 #warn "line<$line>\n";
6c72ffaa 1500
d8aaf121
AW
1501 } elsif ($realcnt == 1) {
1502 $realcnt--;
0a920b5b
AW
1503 }
1504
cc77cdca
AW
1505 my $hunk_line = ($realcnt != 0);
1506
0a920b5b 1507#make up the handle for any error we report on this line
773647a0
AW
1508 $prefix = "$filename:$realline: " if ($emacs && $file);
1509 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1510
6c72ffaa
AW
1511 $here = "#$linenr: " if (!$file);
1512 $here = "#$realline: " if ($file);
773647a0
AW
1513
1514 # extract the filename as it passes
3bf9a009
RV
1515 if ($line =~ /^diff --git.*?(\S+)$/) {
1516 $realfile = $1;
1517 $realfile =~ s@^([^/]*)/@@;
270c49a0 1518 $in_commit_log = 0;
3bf9a009 1519 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
773647a0 1520 $realfile = $1;
1e855726 1521 $realfile =~ s@^([^/]*)/@@;
270c49a0 1522 $in_commit_log = 0;
1e855726
WS
1523
1524 $p1_prefix = $1;
e2f7aa4b
AW
1525 if (!$file && $tree && $p1_prefix ne '' &&
1526 -e "$root/$p1_prefix") {
000d1cc1
JP
1527 WARN("PATCH_PREFIX",
1528 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1e855726 1529 }
773647a0 1530
c1ab3326 1531 if ($realfile =~ m@^include/asm/@) {
000d1cc1
JP
1532 ERROR("MODIFIED_INCLUDE_ASM",
1533 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
773647a0
AW
1534 }
1535 next;
1536 }
1537
389834b6 1538 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
0a920b5b 1539
c2fdda0d
AW
1540 my $hereline = "$here\n$rawline\n";
1541 my $herecurr = "$here\n$rawline\n";
1542 my $hereprev = "$here\n$prevrawline\n$rawline\n";
0a920b5b 1543
6c72ffaa
AW
1544 $cnt_lines++ if ($realcnt != 0);
1545
3bf9a009
RV
1546# Check for incorrect file permissions
1547 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1548 my $permhere = $here . "FILE: $realfile\n";
1549 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
000d1cc1
JP
1550 ERROR("EXECUTE_PERMISSIONS",
1551 "do not set execute permissions for source files\n" . $permhere);
3bf9a009
RV
1552 }
1553 }
1554
20112475 1555# Check the patch for a signoff:
d8aaf121 1556 if ($line =~ /^\s*signed-off-by:/i) {
4a0df2ef 1557 $signoff++;
15662b3e 1558 $in_commit_log = 0;
20112475
JP
1559 }
1560
1561# Check signature styles
270c49a0
JP
1562 if (!$in_header_lines &&
1563 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
20112475
JP
1564 my $space_before = $1;
1565 my $sign_off = $2;
1566 my $space_after = $3;
1567 my $email = $4;
1568 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1569
1570 if (defined $space_before && $space_before ne "") {
000d1cc1
JP
1571 WARN("BAD_SIGN_OFF",
1572 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
20112475
JP
1573 }
1574 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
000d1cc1
JP
1575 WARN("BAD_SIGN_OFF",
1576 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
20112475
JP
1577 }
1578 if (!defined $space_after || $space_after ne " ") {
000d1cc1
JP
1579 WARN("BAD_SIGN_OFF",
1580 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
0a920b5b 1581 }
20112475
JP
1582
1583 my ($email_name, $email_address, $comment) = parse_email($email);
1584 my $suggested_email = format_email(($email_name, $email_address));
1585 if ($suggested_email eq "") {
000d1cc1
JP
1586 ERROR("BAD_SIGN_OFF",
1587 "Unrecognized email address: '$email'\n" . $herecurr);
20112475
JP
1588 } else {
1589 my $dequoted = $suggested_email;
1590 $dequoted =~ s/^"//;
1591 $dequoted =~ s/" </ </;
1592 # Don't force email to have quotes
1593 # Allow just an angle bracketed address
1594 if ("$dequoted$comment" ne $email &&
1595 "<$email_address>$comment" ne $email &&
1596 "$suggested_email$comment" ne $email) {
000d1cc1
JP
1597 WARN("BAD_SIGN_OFF",
1598 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
20112475 1599 }
0a920b5b
AW
1600 }
1601 }
1602
00df344f 1603# Check for wrappage within a valid hunk of the file
8905a67c 1604 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
000d1cc1
JP
1605 ERROR("CORRUPTED_PATCH",
1606 "patch seems to be corrupt (line wrapped?)\n" .
6c72ffaa 1607 $herecurr) if (!$emitted_corrupt++);
de7d4f0e
AW
1608 }
1609
6ecd9674
AW
1610# Check for absolute kernel paths.
1611 if ($tree) {
1612 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1613 my $file = $1;
1614
1615 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1616 check_absolute_file($1, $herecurr)) {
1617 #
1618 } else {
1619 check_absolute_file($file, $herecurr);
1620 }
1621 }
1622 }
1623
de7d4f0e
AW
1624# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1625 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
171ae1a4
AW
1626 $rawline !~ m/^$UTF8*$/) {
1627 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1628
1629 my $blank = copy_spacing($rawline);
1630 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1631 my $hereptr = "$hereline$ptr\n";
1632
34d99219
JP
1633 CHK("INVALID_UTF8",
1634 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
00df344f
AW
1635 }
1636
15662b3e
JP
1637# Check if it's the start of a commit log
1638# (not a header line and we haven't seen the patch filename)
1639 if ($in_header_lines && $realfile =~ /^$/ &&
270c49a0 1640 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
15662b3e
JP
1641 $in_header_lines = 0;
1642 $in_commit_log = 1;
1643 }
1644
1645# Still not yet in a patch, check for any UTF-8
1646 if ($in_commit_log && $realfile =~ /^$/ &&
1647 $rawline =~ /$NON_ASCII_UTF8/) {
1648 CHK("UTF8_BEFORE_PATCH",
1649 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1650 }
1651
30670854
AW
1652# ignore non-hunk lines and lines being removed
1653 next if (!$hunk_line || $line =~ /^-/);
0a920b5b 1654
0a920b5b 1655#trailing whitespace
9c0ca6f9 1656 if ($line =~ /^\+.*\015/) {
c2fdda0d 1657 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
000d1cc1
JP
1658 ERROR("DOS_LINE_ENDINGS",
1659 "DOS line endings\n" . $herevet);
9c0ca6f9 1660
c2fdda0d
AW
1661 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1662 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
000d1cc1
JP
1663 ERROR("TRAILING_WHITESPACE",
1664 "trailing whitespace\n" . $herevet);
d2c0a235 1665 $rpt_cleaners = 1;
0a920b5b 1666 }
5368df20 1667
3354957a 1668# check for Kconfig help text having a real description
9fe287d7
AW
1669# Only applies when adding the entry originally, after that we do not have
1670# sufficient context to determine whether it is indeed long enough.
3354957a 1671 if ($realfile =~ /Kconfig/ &&
9fe287d7 1672 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
3354957a 1673 my $length = 0;
9fe287d7
AW
1674 my $cnt = $realcnt;
1675 my $ln = $linenr + 1;
1676 my $f;
1677 my $is_end = 0;
1678 while ($cnt > 0 && defined $lines[$ln - 1]) {
1679 $f = $lines[$ln - 1];
1680 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1681 $is_end = $lines[$ln - 1] =~ /^\+/;
1682 $ln++;
1683
1684 next if ($f =~ /^-/);
1685 $f =~ s/^.//;
3354957a
AK
1686 $f =~ s/#.*//;
1687 $f =~ s/^\s+//;
1688 next if ($f =~ /^$/);
9fe287d7
AW
1689 if ($f =~ /^\s*config\s/) {
1690 $is_end = 1;
1691 last;
1692 }
3354957a
AK
1693 $length++;
1694 }
000d1cc1
JP
1695 WARN("CONFIG_DESCRIPTION",
1696 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
9fe287d7 1697 #print "is_end<$is_end> length<$length>\n";
3354957a
AK
1698 }
1699
c68e5878
AL
1700 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1701 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1702 my $flag = $1;
1703 my $replacement = {
1704 'EXTRA_AFLAGS' => 'asflags-y',
1705 'EXTRA_CFLAGS' => 'ccflags-y',
1706 'EXTRA_CPPFLAGS' => 'cppflags-y',
1707 'EXTRA_LDFLAGS' => 'ldflags-y',
1708 };
1709
1710 WARN("DEPRECATED_VARIABLE",
1711 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1712 }
1713
5368df20
AW
1714# check we are in a valid source file if not then ignore this hunk
1715 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1716
0a920b5b 1717#80 column limit
c45dcabd 1718 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
f4c014c0 1719 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
0fccc622 1720 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
8bbea968 1721 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
f4c014c0 1722 $length > 80)
c45dcabd 1723 {
000d1cc1
JP
1724 WARN("LONG_LINE",
1725 "line over 80 characters\n" . $herecurr);
0a920b5b
AW
1726 }
1727
5e79d96e
JP
1728# check for spaces before a quoted newline
1729 if ($rawline =~ /^.*\".*\s\\n/) {
000d1cc1
JP
1730 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1731 "unnecessary whitespace before a quoted newline\n" . $herecurr);
5e79d96e
JP
1732 }
1733
8905a67c
AW
1734# check for adding lines without a newline.
1735 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
000d1cc1
JP
1736 WARN("MISSING_EOF_NEWLINE",
1737 "adding a line without newline at end of file\n" . $herecurr);
8905a67c
AW
1738 }
1739
42e41c54
MF
1740# Blackfin: use hi/lo macros
1741 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1742 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1743 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
1744 ERROR("LO_MACRO",
1745 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
42e41c54
MF
1746 }
1747 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1748 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
1749 ERROR("HI_MACRO",
1750 "use the HI() macro, not (... >> 16)\n" . $herevet);
42e41c54
MF
1751 }
1752 }
1753
b9ea10d6
AW
1754# check we are in a valid source file C or perl if not then ignore this hunk
1755 next if ($realfile !~ /\.(h|c|pl)$/);
0a920b5b
AW
1756
1757# at the beginning of a line any tabs must come first and anything
1758# more than 8 must use tabs.
c2fdda0d
AW
1759 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1760 $rawline =~ /^\+\s* \s*/) {
1761 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
000d1cc1
JP
1762 ERROR("CODE_INDENT",
1763 "code indent should use tabs where possible\n" . $herevet);
d2c0a235 1764 $rpt_cleaners = 1;
0a920b5b
AW
1765 }
1766
08e44365
AP
1767# check for space before tabs.
1768 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1769 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
000d1cc1
JP
1770 WARN("SPACE_BEFORE_TAB",
1771 "please, no space before tabs\n" . $herevet);
08e44365
AP
1772 }
1773
5f7ddae6 1774# check for spaces at the beginning of a line.
6b4c5beb
AW
1775# Exceptions:
1776# 1) within comments
1777# 2) indented preprocessor commands
1778# 3) hanging labels
1779 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
5f7ddae6 1780 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
000d1cc1
JP
1781 WARN("LEADING_SPACE",
1782 "please, no spaces at the start of a line\n" . $herevet);
5f7ddae6
RR
1783 }
1784
b9ea10d6
AW
1785# check we are in a valid C source file if not then ignore this hunk
1786 next if ($realfile !~ /\.(h|c)$/);
1787
c2fdda0d 1788# check for RCS/CVS revision markers
cf655043 1789 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
000d1cc1
JP
1790 WARN("CVS_KEYWORD",
1791 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
c2fdda0d 1792 }
22f2a2ef 1793
42e41c54
MF
1794# Blackfin: don't use __builtin_bfin_[cs]sync
1795 if ($line =~ /__builtin_bfin_csync/) {
1796 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
1797 ERROR("CSYNC",
1798 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
42e41c54
MF
1799 }
1800 if ($line =~ /__builtin_bfin_ssync/) {
1801 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
1802 ERROR("SSYNC",
1803 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
42e41c54
MF
1804 }
1805
9c0ca6f9 1806# Check for potential 'bare' types
2b474a1a
AW
1807 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1808 $realline_next);
9c9ba34e 1809 if ($realcnt && $line =~ /.\s*\S/) {
170d3a22 1810 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
f5fe35dd 1811 ctx_statement_block($linenr, $realcnt, 0);
171ae1a4
AW
1812 $stat =~ s/\n./\n /g;
1813 $cond =~ s/\n./\n /g;
1814
f74bd194
AW
1815#print "stat<$stat>\n";
1816
2b474a1a
AW
1817 # Find the real next line.
1818 $realline_next = $line_nr_next;
1819 if (defined $realline_next &&
1820 (!defined $lines[$realline_next - 1] ||
1821 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1822 $realline_next++;
1823 }
1824
171ae1a4
AW
1825 my $s = $stat;
1826 $s =~ s/{.*$//s;
cf655043 1827
c2fdda0d 1828 # Ignore goto labels.
171ae1a4 1829 if ($s =~ /$Ident:\*$/s) {
c2fdda0d
AW
1830
1831 # Ignore functions being called
171ae1a4 1832 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
c2fdda0d 1833
463f2864
AW
1834 } elsif ($s =~ /^.\s*else\b/s) {
1835
c45dcabd 1836 # declarations always start with types
d2506586 1837 } 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
1838 my $type = $1;
1839 $type =~ s/\s+/ /g;
1840 possible($type, "A:" . $s);
1841
8905a67c 1842 # definitions in global scope can only start with types
a6a84062 1843 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
c45dcabd 1844 possible($1, "B:" . $s);
c2fdda0d 1845 }
8905a67c
AW
1846
1847 # any (foo ... *) is a pointer cast, and foo is a type
65863862 1848 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
c45dcabd 1849 possible($1, "C:" . $s);
8905a67c
AW
1850 }
1851
1852 # Check for any sort of function declaration.
1853 # int foo(something bar, other baz);
1854 # void (*store_gdt)(x86_descr_ptr *);
171ae1a4 1855 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
8905a67c 1856 my ($name_len) = length($1);
8905a67c 1857
cf655043 1858 my $ctx = $s;
773647a0 1859 substr($ctx, 0, $name_len + 1, '');
8905a67c 1860 $ctx =~ s/\)[^\)]*$//;
cf655043 1861
8905a67c 1862 for my $arg (split(/\s*,\s*/, $ctx)) {
c45dcabd 1863 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
8905a67c 1864
c45dcabd 1865 possible($1, "D:" . $s);
8905a67c
AW
1866 }
1867 }
9c0ca6f9 1868 }
8905a67c 1869
9c0ca6f9
AW
1870 }
1871
653d4876
AW
1872#
1873# Checks which may be anchored in the context.
1874#
00df344f 1875
653d4876
AW
1876# Check for switch () and associated case and default
1877# statements should be at the same indent.
00df344f
AW
1878 if ($line=~/\bswitch\s*\(.*\)/) {
1879 my $err = '';
1880 my $sep = '';
1881 my @ctx = ctx_block_outer($linenr, $realcnt);
1882 shift(@ctx);
1883 for my $ctx (@ctx) {
1884 my ($clen, $cindent) = line_stats($ctx);
1885 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1886 $indent != $cindent) {
1887 $err .= "$sep$ctx\n";
1888 $sep = '';
1889 } else {
1890 $sep = "[...]\n";
1891 }
1892 }
1893 if ($err ne '') {
000d1cc1
JP
1894 ERROR("SWITCH_CASE_INDENT_LEVEL",
1895 "switch and case should be at the same indent\n$hereline$err");
de7d4f0e
AW
1896 }
1897 }
1898
1899# if/while/etc brace do not go on next line, unless defining a do while loop,
1900# or if that brace on the next line is for something else
c45dcabd 1901 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
773647a0
AW
1902 my $pre_ctx = "$1$2";
1903
9c0ca6f9 1904 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
de7d4f0e
AW
1905 my $ctx_cnt = $realcnt - $#ctx - 1;
1906 my $ctx = join("\n", @ctx);
1907
548596d5
AW
1908 my $ctx_ln = $linenr;
1909 my $ctx_skip = $realcnt;
773647a0 1910
548596d5
AW
1911 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1912 defined $lines[$ctx_ln - 1] &&
1913 $lines[$ctx_ln - 1] =~ /^-/)) {
1914 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1915 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
de7d4f0e 1916 $ctx_ln++;
de7d4f0e 1917 }
548596d5 1918
53210168
AW
1919 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1920 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
de7d4f0e 1921
773647a0 1922 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
000d1cc1
JP
1923 ERROR("OPEN_BRACE",
1924 "that open brace { should be on the previous line\n" .
01464f30 1925 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
00df344f 1926 }
773647a0
AW
1927 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1928 $ctx =~ /\)\s*\;\s*$/ &&
1929 defined $lines[$ctx_ln - 1])
1930 {
9c0ca6f9
AW
1931 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1932 if ($nindent > $indent) {
000d1cc1
JP
1933 WARN("TRAILING_SEMICOLON",
1934 "trailing semicolon indicates no statements, indent implies otherwise\n" .
01464f30 1935 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
9c0ca6f9
AW
1936 }
1937 }
00df344f
AW
1938 }
1939
4d001e4d
AW
1940# Check relative indent for conditionals and blocks.
1941 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1942 my ($s, $c) = ($stat, $cond);
1943
1944 substr($s, 0, length($c), '');
1945
1946 # Make sure we remove the line prefixes as we have
1947 # none on the first line, and are going to readd them
1948 # where necessary.
1949 $s =~ s/\n./\n/gs;
1950
1951 # Find out how long the conditional actually is.
6f779c18
AW
1952 my @newlines = ($c =~ /\n/gs);
1953 my $cond_lines = 1 + $#newlines;
4d001e4d
AW
1954
1955 # We want to check the first line inside the block
1956 # starting at the end of the conditional, so remove:
1957 # 1) any blank line termination
1958 # 2) any opening brace { on end of the line
1959 # 3) any do (...) {
1960 my $continuation = 0;
1961 my $check = 0;
1962 $s =~ s/^.*\bdo\b//;
1963 $s =~ s/^\s*{//;
1964 if ($s =~ s/^\s*\\//) {
1965 $continuation = 1;
1966 }
9bd49efe 1967 if ($s =~ s/^\s*?\n//) {
4d001e4d
AW
1968 $check = 1;
1969 $cond_lines++;
1970 }
1971
1972 # Also ignore a loop construct at the end of a
1973 # preprocessor statement.
1974 if (($prevline =~ /^.\s*#\s*define\s/ ||
1975 $prevline =~ /\\\s*$/) && $continuation == 0) {
1976 $check = 0;
1977 }
1978
9bd49efe 1979 my $cond_ptr = -1;
740504c6 1980 $continuation = 0;
9bd49efe
AW
1981 while ($cond_ptr != $cond_lines) {
1982 $cond_ptr = $cond_lines;
1983
f16fa28f
AW
1984 # If we see an #else/#elif then the code
1985 # is not linear.
1986 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1987 $check = 0;
1988 }
1989
9bd49efe
AW
1990 # Ignore:
1991 # 1) blank lines, they should be at 0,
1992 # 2) preprocessor lines, and
1993 # 3) labels.
740504c6
AW
1994 if ($continuation ||
1995 $s =~ /^\s*?\n/ ||
9bd49efe
AW
1996 $s =~ /^\s*#\s*?/ ||
1997 $s =~ /^\s*$Ident\s*:/) {
740504c6 1998 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
30dad6eb
AW
1999 if ($s =~ s/^.*?\n//) {
2000 $cond_lines++;
2001 }
9bd49efe 2002 }
4d001e4d
AW
2003 }
2004
2005 my (undef, $sindent) = line_stats("+" . $s);
2006 my $stat_real = raw_line($linenr, $cond_lines);
2007
2008 # Check if either of these lines are modified, else
2009 # this is not this patch's fault.
2010 if (!defined($stat_real) ||
2011 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2012 $check = 0;
2013 }
2014 if (defined($stat_real) && $cond_lines > 1) {
2015 $stat_real = "[...]\n$stat_real";
2016 }
2017
9bd49efe 2018 #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
2019
2020 if ($check && (($sindent % 8) != 0 ||
2021 ($sindent <= $indent && $s ne ''))) {
000d1cc1
JP
2022 WARN("SUSPECT_CODE_INDENT",
2023 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
4d001e4d
AW
2024 }
2025 }
2026
6c72ffaa
AW
2027 # Track the 'values' across context and added lines.
2028 my $opline = $line; $opline =~ s/^./ /;
1f65f947
AW
2029 my ($curr_values, $curr_vars) =
2030 annotate_values($opline . "\n", $prev_values);
6c72ffaa 2031 $curr_values = $prev_values . $curr_values;
c2fdda0d
AW
2032 if ($dbg_values) {
2033 my $outline = $opline; $outline =~ s/\t/ /g;
cf655043
AW
2034 print "$linenr > .$outline\n";
2035 print "$linenr > $curr_values\n";
1f65f947 2036 print "$linenr > $curr_vars\n";
c2fdda0d 2037 }
6c72ffaa
AW
2038 $prev_values = substr($curr_values, -1);
2039
00df344f
AW
2040#ignore lines not being added
2041 if ($line=~/^[^\+]/) {next;}
2042
653d4876 2043# TEST: allow direct testing of the type matcher.
7429c690
AW
2044 if ($dbg_type) {
2045 if ($line =~ /^.\s*$Declare\s*$/) {
000d1cc1
JP
2046 ERROR("TEST_TYPE",
2047 "TEST: is type\n" . $herecurr);
7429c690 2048 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
000d1cc1
JP
2049 ERROR("TEST_NOT_TYPE",
2050 "TEST: is not type ($1 is)\n". $herecurr);
7429c690 2051 }
653d4876
AW
2052 next;
2053 }
a1ef277e
AW
2054# TEST: allow direct testing of the attribute matcher.
2055 if ($dbg_attr) {
9360b0e5 2056 if ($line =~ /^.\s*$Modifier\s*$/) {
000d1cc1
JP
2057 ERROR("TEST_ATTR",
2058 "TEST: is attr\n" . $herecurr);
9360b0e5 2059 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
000d1cc1
JP
2060 ERROR("TEST_NOT_ATTR",
2061 "TEST: is not attr ($1 is)\n". $herecurr);
a1ef277e
AW
2062 }
2063 next;
2064 }
653d4876 2065
f0a594c1 2066# check for initialisation to aggregates open brace on the next line
99423c20
AW
2067 if ($line =~ /^.\s*{/ &&
2068 $prevline =~ /(?:^|[^=])=\s*$/) {
000d1cc1
JP
2069 ERROR("OPEN_BRACE",
2070 "that open brace { should be on the previous line\n" . $hereprev);
f0a594c1
AW
2071 }
2072
653d4876
AW
2073#
2074# Checks which are anchored on the added line.
2075#
2076
2077# check for malformed paths in #include statements (uses RAW line)
c45dcabd 2078 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
653d4876
AW
2079 my $path = $1;
2080 if ($path =~ m{//}) {
000d1cc1
JP
2081 ERROR("MALFORMED_INCLUDE",
2082 "malformed #include filename\n" .
de7d4f0e 2083 $herecurr);
653d4876 2084 }
653d4876 2085 }
00df344f 2086
0a920b5b 2087# no C99 // comments
00df344f 2088 if ($line =~ m{//}) {
000d1cc1
JP
2089 ERROR("C99_COMMENTS",
2090 "do not use C99 // comments\n" . $herecurr);
0a920b5b 2091 }
00df344f 2092 # Remove C99 comments.
0a920b5b 2093 $line =~ s@//.*@@;
6c72ffaa 2094 $opline =~ s@//.*@@;
0a920b5b 2095
2b474a1a
AW
2096# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2097# the whole statement.
2098#print "APW <$lines[$realline_next - 1]>\n";
2099 if (defined $realline_next &&
2100 exists $lines[$realline_next - 1] &&
2101 !defined $suppress_export{$realline_next} &&
2102 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2103 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3cbf62df
AW
2104 # Handle definitions which produce identifiers with
2105 # a prefix:
2106 # XXX(foo);
2107 # EXPORT_SYMBOL(something_foo);
653d4876 2108 my $name = $1;
3cbf62df
AW
2109 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
2110 $name =~ /^${Ident}_$2/) {
2111#print "FOO C name<$name>\n";
2112 $suppress_export{$realline_next} = 1;
2113
2114 } elsif ($stat !~ /(?:
2b474a1a 2115 \n.}\s*$|
48012058
AW
2116 ^.DEFINE_$Ident\(\Q$name\E\)|
2117 ^.DECLARE_$Ident\(\Q$name\E\)|
2118 ^.LIST_HEAD\(\Q$name\E\)|
2b474a1a
AW
2119 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2120 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
48012058 2121 )/x) {
2b474a1a
AW
2122#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2123 $suppress_export{$realline_next} = 2;
2124 } else {
2125 $suppress_export{$realline_next} = 1;
0a920b5b
AW
2126 }
2127 }
2b474a1a
AW
2128 if (!defined $suppress_export{$linenr} &&
2129 $prevline =~ /^.\s*$/ &&
2130 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2131 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2132#print "FOO B <$lines[$linenr - 1]>\n";
2133 $suppress_export{$linenr} = 2;
2134 }
2135 if (defined $suppress_export{$linenr} &&
2136 $suppress_export{$linenr} == 2) {
000d1cc1
JP
2137 WARN("EXPORT_SYMBOL",
2138 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2b474a1a 2139 }
0a920b5b 2140
5150bda4 2141# check for global initialisers.
c45dcabd 2142 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
000d1cc1
JP
2143 ERROR("GLOBAL_INITIALISERS",
2144 "do not initialise globals to 0 or NULL\n" .
f0a594c1
AW
2145 $herecurr);
2146 }
653d4876 2147# check for static initialisers.
2d1bafd7 2148 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
000d1cc1
JP
2149 ERROR("INITIALISED_STATIC",
2150 "do not initialise statics to 0 or NULL\n" .
de7d4f0e 2151 $herecurr);
0a920b5b
AW
2152 }
2153
cb710eca
JP
2154# check for static const char * arrays.
2155 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
000d1cc1
JP
2156 WARN("STATIC_CONST_CHAR_ARRAY",
2157 "static const char * array should probably be static const char * const\n" .
cb710eca
JP
2158 $herecurr);
2159 }
2160
2161# check for static char foo[] = "bar" declarations.
2162 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
000d1cc1
JP
2163 WARN("STATIC_CONST_CHAR_ARRAY",
2164 "static char array declaration should probably be static const char\n" .
cb710eca
JP
2165 $herecurr);
2166 }
2167
93ed0e2d
JP
2168# check for declarations of struct pci_device_id
2169 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
000d1cc1
JP
2170 WARN("DEFINE_PCI_DEVICE_TABLE",
2171 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
93ed0e2d
JP
2172 }
2173
653d4876
AW
2174# check for new typedefs, only function parameters and sparse annotations
2175# make sense.
2176 if ($line =~ /\btypedef\s/ &&
8054576d 2177 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
c45dcabd 2178 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
8ed22cad 2179 $line !~ /\b$typeTypedefs\b/ &&
653d4876 2180 $line !~ /\b__bitwise(?:__|)\b/) {
000d1cc1
JP
2181 WARN("NEW_TYPEDEFS",
2182 "do not add new typedefs\n" . $herecurr);
0a920b5b
AW
2183 }
2184
2185# * goes on variable not on type
65863862 2186 # (char*[ const])
00ef4ece 2187 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
65863862
AW
2188 my ($from, $to) = ($1, $1);
2189
2190 # Should start with a space.
2191 $to =~ s/^(\S)/ $1/;
2192 # Should not end with a space.
2193 $to =~ s/\s+$//;
2194 # '*'s should not have spaces between.
f9a0b3d1 2195 while ($to =~ s/\*\s+\*/\*\*/) {
65863862 2196 }
d8aaf121 2197
65863862
AW
2198 #print "from<$from> to<$to>\n";
2199 if ($from ne $to) {
000d1cc1
JP
2200 ERROR("POINTER_LOCATION",
2201 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
65863862 2202 }
00ef4ece 2203 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
65863862
AW
2204 my ($from, $to, $ident) = ($1, $1, $2);
2205
2206 # Should start with a space.
2207 $to =~ s/^(\S)/ $1/;
2208 # Should not end with a space.
2209 $to =~ s/\s+$//;
2210 # '*'s should not have spaces between.
f9a0b3d1 2211 while ($to =~ s/\*\s+\*/\*\*/) {
65863862
AW
2212 }
2213 # Modifiers should have spaces.
2214 $to =~ s/(\b$Modifier$)/$1 /;
d8aaf121 2215
667026e7
AW
2216 #print "from<$from> to<$to> ident<$ident>\n";
2217 if ($from ne $to && $ident !~ /^$Modifier$/) {
000d1cc1
JP
2218 ERROR("POINTER_LOCATION",
2219 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
65863862 2220 }
0a920b5b
AW
2221 }
2222
2223# # no BUG() or BUG_ON()
2224# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2225# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2226# print "$herecurr";
2227# $clean = 0;
2228# }
2229
8905a67c 2230 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
000d1cc1
JP
2231 WARN("LINUX_VERSION_CODE",
2232 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
8905a67c
AW
2233 }
2234
17441227
JP
2235# check for uses of printk_ratelimit
2236 if ($line =~ /\bprintk_ratelimit\s*\(/) {
000d1cc1
JP
2237 WARN("PRINTK_RATELIMITED",
2238"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
17441227
JP
2239 }
2240
00df344f
AW
2241# printk should use KERN_* levels. Note that follow on printk's on the
2242# same line do not need a level, so we use the current block context
2243# to try and find and validate the current printk. In summary the current
25985edc 2244# printk includes all preceding printk's which have no newline on the end.
00df344f 2245# we assume the first bad printk is the one to report.
f0a594c1 2246 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
00df344f
AW
2247 my $ok = 0;
2248 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2249 #print "CHECK<$lines[$ln - 1]\n";
25985edc 2250 # we have a preceding printk if it ends
00df344f
AW
2251 # with "\n" ignore it, else it is to blame
2252 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2253 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2254 $ok = 1;
2255 }
2256 last;
2257 }
2258 }
2259 if ($ok == 0) {
000d1cc1
JP
2260 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2261 "printk() should include KERN_ facility level\n" . $herecurr);
00df344f 2262 }
0a920b5b
AW
2263 }
2264
653d4876
AW
2265# function brace can't be on same line, except for #defines of do while,
2266# or if closed on same line
c45dcabd
AW
2267 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2268 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
000d1cc1
JP
2269 ERROR("OPEN_BRACE",
2270 "open brace '{' following function declarations go on the next line\n" . $herecurr);
0a920b5b 2271 }
653d4876 2272
8905a67c
AW
2273# open braces for enum, union and struct go on the same line.
2274 if ($line =~ /^.\s*{/ &&
2275 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
000d1cc1
JP
2276 ERROR("OPEN_BRACE",
2277 "open brace '{' following $1 go on the same line\n" . $hereprev);
8905a67c
AW
2278 }
2279
0c73b4eb
AW
2280# missing space after union, struct or enum definition
2281 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
000d1cc1
JP
2282 WARN("SPACING",
2283 "missing space after $1 definition\n" . $herecurr);
0c73b4eb
AW
2284 }
2285
8d31cfce
AW
2286# check for spacing round square brackets; allowed:
2287# 1. with a type on the left -- int [] a;
fe2a7dbc
AW
2288# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2289# 3. inside a curly brace -- = { [0...10] = 5 }
8d31cfce
AW
2290 while ($line =~ /(.*?\s)\[/g) {
2291 my ($where, $prefix) = ($-[1], $1);
2292 if ($prefix !~ /$Type\s+$/ &&
fe2a7dbc
AW
2293 ($where != 0 || $prefix !~ /^.\s+$/) &&
2294 $prefix !~ /{\s+$/) {
000d1cc1
JP
2295 ERROR("BRACKET_SPACE",
2296 "space prohibited before open square bracket '['\n" . $herecurr);
8d31cfce
AW
2297 }
2298 }
2299
f0a594c1 2300# check for spaces between functions and their parentheses.
6c72ffaa 2301 while ($line =~ /($Ident)\s+\(/g) {
c2fdda0d 2302 my $name = $1;
773647a0
AW
2303 my $ctx_before = substr($line, 0, $-[1]);
2304 my $ctx = "$ctx_before$name";
c2fdda0d
AW
2305
2306 # Ignore those directives where spaces _are_ permitted.
773647a0
AW
2307 if ($name =~ /^(?:
2308 if|for|while|switch|return|case|
2309 volatile|__volatile__|
2310 __attribute__|format|__extension__|
2311 asm|__asm__)$/x)
2312 {
c2fdda0d
AW
2313
2314 # cpp #define statements have non-optional spaces, ie
2315 # if there is a space between the name and the open
2316 # parenthesis it is simply not a parameter group.
c45dcabd 2317 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
773647a0
AW
2318
2319 # cpp #elif statement condition may start with a (
c45dcabd 2320 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
c2fdda0d
AW
2321
2322 # If this whole things ends with a type its most
2323 # likely a typedef for a function.
773647a0 2324 } elsif ($ctx =~ /$Type$/) {
c2fdda0d
AW
2325
2326 } else {
000d1cc1
JP
2327 WARN("SPACING",
2328 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
6c72ffaa 2329 }
f0a594c1 2330 }
653d4876 2331# Check operator spacing.
0a920b5b 2332 if (!($line=~/\#\s*include/)) {
9c0ca6f9
AW
2333 my $ops = qr{
2334 <<=|>>=|<=|>=|==|!=|
2335 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2336 =>|->|<<|>>|<|>|=|!|~|
1f65f947
AW
2337 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2338 \?|:
9c0ca6f9 2339 }x;
cf655043 2340 my @elements = split(/($ops|;)/, $opline);
00df344f 2341 my $off = 0;
6c72ffaa
AW
2342
2343 my $blank = copy_spacing($opline);
2344
0a920b5b 2345 for (my $n = 0; $n < $#elements; $n += 2) {
4a0df2ef
AW
2346 $off += length($elements[$n]);
2347
25985edc 2348 # Pick up the preceding and succeeding characters.
773647a0
AW
2349 my $ca = substr($opline, 0, $off);
2350 my $cc = '';
2351 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2352 $cc = substr($opline, $off + length($elements[$n + 1]));
2353 }
2354 my $cb = "$ca$;$cc";
2355
4a0df2ef
AW
2356 my $a = '';
2357 $a = 'V' if ($elements[$n] ne '');
2358 $a = 'W' if ($elements[$n] =~ /\s$/);
cf655043 2359 $a = 'C' if ($elements[$n] =~ /$;$/);
4a0df2ef
AW
2360 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2361 $a = 'O' if ($elements[$n] eq '');
773647a0 2362 $a = 'E' if ($ca =~ /^\s*$/);
4a0df2ef 2363
0a920b5b 2364 my $op = $elements[$n + 1];
4a0df2ef
AW
2365
2366 my $c = '';
0a920b5b 2367 if (defined $elements[$n + 2]) {
4a0df2ef
AW
2368 $c = 'V' if ($elements[$n + 2] ne '');
2369 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
cf655043 2370 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4a0df2ef
AW
2371 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2372 $c = 'O' if ($elements[$n + 2] eq '');
8b1b3378 2373 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4a0df2ef
AW
2374 } else {
2375 $c = 'E';
0a920b5b
AW
2376 }
2377
4a0df2ef
AW
2378 my $ctx = "${a}x${c}";
2379
2380 my $at = "(ctx:$ctx)";
2381
6c72ffaa 2382 my $ptr = substr($blank, 0, $off) . "^";
de7d4f0e 2383 my $hereptr = "$hereline$ptr\n";
0a920b5b 2384
74048ed8 2385 # Pull out the value of this operator.
6c72ffaa 2386 my $op_type = substr($curr_values, $off + 1, 1);
0a920b5b 2387
1f65f947
AW
2388 # Get the full operator variant.
2389 my $opv = $op . substr($curr_vars, $off, 1);
2390
13214adf
AW
2391 # Ignore operators passed as parameters.
2392 if ($op_type ne 'V' &&
2393 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2394
cf655043
AW
2395# # Ignore comments
2396# } elsif ($op =~ /^$;+$/) {
13214adf 2397
d8aaf121 2398 # ; should have either the end of line or a space or \ after it
13214adf 2399 } elsif ($op eq ';') {
cf655043
AW
2400 if ($ctx !~ /.x[WEBC]/ &&
2401 $cc !~ /^\\/ && $cc !~ /^;/) {
000d1cc1
JP
2402 ERROR("SPACING",
2403 "space required after that '$op' $at\n" . $hereptr);
d8aaf121
AW
2404 }
2405
2406 # // is a comment
2407 } elsif ($op eq '//') {
0a920b5b 2408
1f65f947
AW
2409 # No spaces for:
2410 # ->
2411 # : when part of a bitfield
2412 } elsif ($op eq '->' || $opv eq ':B') {
4a0df2ef 2413 if ($ctx =~ /Wx.|.xW/) {
000d1cc1
JP
2414 ERROR("SPACING",
2415 "spaces prohibited around that '$op' $at\n" . $hereptr);
0a920b5b
AW
2416 }
2417
2418 # , must have a space on the right.
2419 } elsif ($op eq ',') {
cf655043 2420 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
000d1cc1
JP
2421 ERROR("SPACING",
2422 "space required after that '$op' $at\n" . $hereptr);
0a920b5b
AW
2423 }
2424
9c0ca6f9 2425 # '*' as part of a type definition -- reported already.
74048ed8 2426 } elsif ($opv eq '*_') {
9c0ca6f9
AW
2427 #warn "'*' is part of type\n";
2428
2429 # unary operators should have a space before and
2430 # none after. May be left adjacent to another
2431 # unary operator, or a cast
2432 } elsif ($op eq '!' || $op eq '~' ||
74048ed8 2433 $opv eq '*U' || $opv eq '-U' ||
0d413866 2434 $opv eq '&U' || $opv eq '&&U') {
cf655043 2435 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
000d1cc1
JP
2436 ERROR("SPACING",
2437 "space required before that '$op' $at\n" . $hereptr);
0a920b5b 2438 }
a3340b35 2439 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
171ae1a4
AW
2440 # A unary '*' may be const
2441
2442 } elsif ($ctx =~ /.xW/) {
000d1cc1
JP
2443 ERROR("SPACING",
2444 "space prohibited after that '$op' $at\n" . $hereptr);
0a920b5b
AW
2445 }
2446
2447 # unary ++ and unary -- are allowed no space on one side.
2448 } elsif ($op eq '++' or $op eq '--') {
773647a0 2449 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
000d1cc1
JP
2450 ERROR("SPACING",
2451 "space required one side of that '$op' $at\n" . $hereptr);
773647a0
AW
2452 }
2453 if ($ctx =~ /Wx[BE]/ ||
2454 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
000d1cc1
JP
2455 ERROR("SPACING",
2456 "space prohibited before that '$op' $at\n" . $hereptr);
0a920b5b 2457 }
773647a0 2458 if ($ctx =~ /ExW/) {
000d1cc1
JP
2459 ERROR("SPACING",
2460 "space prohibited after that '$op' $at\n" . $hereptr);
653d4876 2461 }
0a920b5b 2462
773647a0 2463
0a920b5b 2464 # << and >> may either have or not have spaces both sides
9c0ca6f9
AW
2465 } elsif ($op eq '<<' or $op eq '>>' or
2466 $op eq '&' or $op eq '^' or $op eq '|' or
2467 $op eq '+' or $op eq '-' or
c2fdda0d
AW
2468 $op eq '*' or $op eq '/' or
2469 $op eq '%')
0a920b5b 2470 {
773647a0 2471 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
000d1cc1
JP
2472 ERROR("SPACING",
2473 "need consistent spacing around '$op' $at\n" .
de7d4f0e 2474 $hereptr);
0a920b5b
AW
2475 }
2476
1f65f947
AW
2477 # A colon needs no spaces before when it is
2478 # terminating a case value or a label.
2479 } elsif ($opv eq ':C' || $opv eq ':L') {
2480 if ($ctx =~ /Wx./) {
000d1cc1
JP
2481 ERROR("SPACING",
2482 "space prohibited before that '$op' $at\n" . $hereptr);
1f65f947
AW
2483 }
2484
0a920b5b 2485 # All the others need spaces both sides.
cf655043 2486 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1f65f947
AW
2487 my $ok = 0;
2488
22f2a2ef 2489 # Ignore email addresses <foo@bar>
1f65f947
AW
2490 if (($op eq '<' &&
2491 $cc =~ /^\S+\@\S+>/) ||
2492 ($op eq '>' &&
2493 $ca =~ /<\S+\@\S+$/))
2494 {
2495 $ok = 1;
2496 }
2497
2498 # Ignore ?:
2499 if (($opv eq ':O' && $ca =~ /\?$/) ||
2500 ($op eq '?' && $cc =~ /^:/)) {
2501 $ok = 1;
2502 }
2503
2504 if ($ok == 0) {
000d1cc1
JP
2505 ERROR("SPACING",
2506 "spaces required around that '$op' $at\n" . $hereptr);
22f2a2ef 2507 }
0a920b5b 2508 }
4a0df2ef 2509 $off += length($elements[$n + 1]);
0a920b5b
AW
2510 }
2511 }
2512
f0a594c1
AW
2513# check for multiple assignments
2514 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
000d1cc1
JP
2515 CHK("MULTIPLE_ASSIGNMENTS",
2516 "multiple assignments should be avoided\n" . $herecurr);
f0a594c1
AW
2517 }
2518
22f2a2ef
AW
2519## # check for multiple declarations, allowing for a function declaration
2520## # continuation.
2521## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2522## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2523##
2524## # Remove any bracketed sections to ensure we do not
2525## # falsly report the parameters of functions.
2526## my $ln = $line;
2527## while ($ln =~ s/\([^\(\)]*\)//g) {
2528## }
2529## if ($ln =~ /,/) {
000d1cc1
JP
2530## WARN("MULTIPLE_DECLARATION",
2531## "declaring multiple variables together should be avoided\n" . $herecurr);
22f2a2ef
AW
2532## }
2533## }
f0a594c1 2534
0a920b5b 2535#need space before brace following if, while, etc
22f2a2ef
AW
2536 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2537 $line =~ /do{/) {
000d1cc1
JP
2538 ERROR("SPACING",
2539 "space required before the open brace '{'\n" . $herecurr);
de7d4f0e
AW
2540 }
2541
2542# closing brace should have a space following it when it has anything
2543# on the line
2544 if ($line =~ /}(?!(?:,|;|\)))\S/) {
000d1cc1
JP
2545 ERROR("SPACING",
2546 "space required after that close brace '}'\n" . $herecurr);
0a920b5b
AW
2547 }
2548
22f2a2ef
AW
2549# check spacing on square brackets
2550 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
000d1cc1
JP
2551 ERROR("SPACING",
2552 "space prohibited after that open square bracket '['\n" . $herecurr);
22f2a2ef
AW
2553 }
2554 if ($line =~ /\s\]/) {
000d1cc1
JP
2555 ERROR("SPACING",
2556 "space prohibited before that close square bracket ']'\n" . $herecurr);
22f2a2ef
AW
2557 }
2558
c45dcabd 2559# check spacing on parentheses
9c0ca6f9
AW
2560 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2561 $line !~ /for\s*\(\s+;/) {
000d1cc1
JP
2562 ERROR("SPACING",
2563 "space prohibited after that open parenthesis '('\n" . $herecurr);
22f2a2ef 2564 }
13214adf 2565 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
c45dcabd
AW
2566 $line !~ /for\s*\(.*;\s+\)/ &&
2567 $line !~ /:\s+\)/) {
000d1cc1
JP
2568 ERROR("SPACING",
2569 "space prohibited before that close parenthesis ')'\n" . $herecurr);
22f2a2ef
AW
2570 }
2571
0a920b5b 2572#goto labels aren't indented, allow a single space however
4a0df2ef 2573 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
0a920b5b 2574 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
000d1cc1
JP
2575 WARN("INDENTED_LABEL",
2576 "labels should not be indented\n" . $herecurr);
0a920b5b
AW
2577 }
2578
c45dcabd
AW
2579# Return is not a function.
2580 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2581 my $spacing = $1;
2582 my $value = $2;
2583
86f9d059 2584 # Flatten any parentheses
fb2d2c1b
AW
2585 $value =~ s/\(/ \(/g;
2586 $value =~ s/\)/\) /g;
63f17f89
AW
2587 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2588 $value !~ /(?:$Ident|-?$Constant)\s*
2589 $Compare\s*
2590 (?:$Ident|-?$Constant)/x &&
2591 $value =~ s/\([^\(\)]*\)/1/) {
c45dcabd 2592 }
fb2d2c1b
AW
2593#print "value<$value>\n";
2594 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
000d1cc1
JP
2595 ERROR("RETURN_PARENTHESES",
2596 "return is not a function, parentheses are not required\n" . $herecurr);
c45dcabd
AW
2597
2598 } elsif ($spacing !~ /\s+/) {
000d1cc1
JP
2599 ERROR("SPACING",
2600 "space required before the open parenthesis '('\n" . $herecurr);
c45dcabd
AW
2601 }
2602 }
53a3c448
AW
2603# Return of what appears to be an errno should normally be -'ve
2604 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2605 my $name = $1;
2606 if ($name ne 'EOF' && $name ne 'ERROR') {
000d1cc1
JP
2607 WARN("USE_NEGATIVE_ERRNO",
2608 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
53a3c448
AW
2609 }
2610 }
c45dcabd 2611
7d2367af
JP
2612# typecasts on min/max could be min_t/max_t
2613 if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2614 if (defined $2 || defined $8) {
2615 my $call = $1;
2616 my $cast1 = deparenthesize($2);
2617 my $arg1 = $3;
2618 my $cast2 = deparenthesize($8);
2619 my $arg2 = $9;
2620 my $cast;
2621
2622 if ($cast1 ne "" && $cast2 ne "") {
2623 $cast = "$cast1 or $cast2";
2624 } elsif ($cast1 ne "") {
2625 $cast = $cast1;
2626 } else {
2627 $cast = $cast2;
2628 }
30ecad51
HZ
2629 WARN("MINMAX",
2630 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
7d2367af
JP
2631 }
2632 }
2633
0a920b5b 2634# Need a space before open parenthesis after if, while etc
4a0df2ef 2635 if ($line=~/\b(if|while|for|switch)\(/) {
000d1cc1 2636 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
0a920b5b
AW
2637 }
2638
f5fe35dd
AW
2639# Check for illegal assignment in if conditional -- and check for trailing
2640# statements after the conditional.
170d3a22
AW
2641 if ($line =~ /do\s*(?!{)/) {
2642 my ($stat_next) = ctx_statement_block($line_nr_next,
2643 $remain_next, $off_next);
2644 $stat_next =~ s/\n./\n /g;
2645 ##print "stat<$stat> stat_next<$stat_next>\n";
2646
2647 if ($stat_next =~ /^\s*while\b/) {
2648 # If the statement carries leading newlines,
2649 # then count those as offsets.
2650 my ($whitespace) =
2651 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2652 my $offset =
2653 statement_rawlines($whitespace) - 1;
2654
2655 $suppress_whiletrailers{$line_nr_next +
2656 $offset} = 1;
2657 }
2658 }
2659 if (!defined $suppress_whiletrailers{$linenr} &&
2660 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
171ae1a4 2661 my ($s, $c) = ($stat, $cond);
8905a67c 2662
b53c8e10 2663 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
000d1cc1
JP
2664 ERROR("ASSIGN_IN_IF",
2665 "do not use assignment in if condition\n" . $herecurr);
8905a67c
AW
2666 }
2667
2668 # Find out what is on the end of the line after the
2669 # conditional.
773647a0 2670 substr($s, 0, length($c), '');
8905a67c 2671 $s =~ s/\n.*//g;
13214adf 2672 $s =~ s/$;//g; # Remove any comments
53210168
AW
2673 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2674 $c !~ /}\s*while\s*/)
773647a0 2675 {
bb44ad39
AW
2676 # Find out how long the conditional actually is.
2677 my @newlines = ($c =~ /\n/gs);
2678 my $cond_lines = 1 + $#newlines;
42bdf74c 2679 my $stat_real = '';
bb44ad39 2680
42bdf74c
HS
2681 $stat_real = raw_line($linenr, $cond_lines)
2682 . "\n" if ($cond_lines);
bb44ad39
AW
2683 if (defined($stat_real) && $cond_lines > 1) {
2684 $stat_real = "[...]\n$stat_real";
2685 }
2686
000d1cc1
JP
2687 ERROR("TRAILING_STATEMENTS",
2688 "trailing statements should be on next line\n" . $herecurr . $stat_real);
8905a67c
AW
2689 }
2690 }
2691
13214adf
AW
2692# Check for bitwise tests written as boolean
2693 if ($line =~ /
2694 (?:
2695 (?:\[|\(|\&\&|\|\|)
2696 \s*0[xX][0-9]+\s*
2697 (?:\&\&|\|\|)
2698 |
2699 (?:\&\&|\|\|)
2700 \s*0[xX][0-9]+\s*
2701 (?:\&\&|\|\||\)|\])
2702 )/x)
2703 {
000d1cc1
JP
2704 WARN("HEXADECIMAL_BOOLEAN_TEST",
2705 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
13214adf
AW
2706 }
2707
8905a67c 2708# if and else should not have general statements after it
13214adf
AW
2709 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2710 my $s = $1;
2711 $s =~ s/$;//g; # Remove any comments
2712 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
000d1cc1
JP
2713 ERROR("TRAILING_STATEMENTS",
2714 "trailing statements should be on next line\n" . $herecurr);
13214adf 2715 }
0a920b5b 2716 }
39667782
AW
2717# if should not continue a brace
2718 if ($line =~ /}\s*if\b/) {
000d1cc1
JP
2719 ERROR("TRAILING_STATEMENTS",
2720 "trailing statements should be on next line\n" .
39667782
AW
2721 $herecurr);
2722 }
a1080bf8
AW
2723# case and default should not have general statements after them
2724 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2725 $line !~ /\G(?:
3fef12d6 2726 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
a1080bf8
AW
2727 \s*return\s+
2728 )/xg)
2729 {
000d1cc1
JP
2730 ERROR("TRAILING_STATEMENTS",
2731 "trailing statements should be on next line\n" . $herecurr);
a1080bf8 2732 }
0a920b5b
AW
2733
2734 # Check for }<nl>else {, these must be at the same
2735 # indent level to be relevant to each other.
2736 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2737 $previndent == $indent) {
000d1cc1
JP
2738 ERROR("ELSE_AFTER_BRACE",
2739 "else should follow close brace '}'\n" . $hereprev);
0a920b5b
AW
2740 }
2741
c2fdda0d
AW
2742 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2743 $previndent == $indent) {
2744 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2745
2746 # Find out what is on the end of the line after the
2747 # conditional.
773647a0 2748 substr($s, 0, length($c), '');
c2fdda0d
AW
2749 $s =~ s/\n.*//g;
2750
2751 if ($s =~ /^\s*;/) {
000d1cc1
JP
2752 ERROR("WHILE_AFTER_BRACE",
2753 "while should follow close brace '}'\n" . $hereprev);
c2fdda0d
AW
2754 }
2755 }
2756
0a920b5b
AW
2757#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2758# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2759# print "No studly caps, use _\n";
2760# print "$herecurr";
2761# $clean = 0;
2762# }
2763
2764#no spaces allowed after \ in define
c45dcabd 2765 if ($line=~/\#\s*define.*\\\s$/) {
000d1cc1
JP
2766 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2767 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
0a920b5b
AW
2768 }
2769
653d4876 2770#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
c45dcabd 2771 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
e09dec48
AW
2772 my $file = "$1.h";
2773 my $checkfile = "include/linux/$file";
2774 if (-f "$root/$checkfile" &&
2775 $realfile ne $checkfile &&
7840a94c 2776 $1 !~ /$allowed_asm_includes/)
c45dcabd 2777 {
e09dec48 2778 if ($realfile =~ m{^arch/}) {
000d1cc1
JP
2779 CHK("ARCH_INCLUDE_LINUX",
2780 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
e09dec48 2781 } else {
000d1cc1
JP
2782 WARN("INCLUDE_LINUX",
2783 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
e09dec48 2784 }
0a920b5b
AW
2785 }
2786 }
2787
653d4876
AW
2788# multi-statement macros should be enclosed in a do while loop, grab the
2789# first statement and ensure its the whole macro if its not enclosed
cf655043 2790# in a known good container
b8f96a31
AW
2791 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2792 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
d8aaf121
AW
2793 my $ln = $linenr;
2794 my $cnt = $realcnt;
c45dcabd
AW
2795 my ($off, $dstat, $dcond, $rest);
2796 my $ctx = '';
c45dcabd 2797 ($dstat, $dcond, $ln, $cnt, $off) =
f74bd194
AW
2798 ctx_statement_block($linenr, $realcnt, 0);
2799 $ctx = $dstat;
c45dcabd 2800 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
a3bb97a7 2801 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
c45dcabd 2802
f74bd194 2803 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
292f1a9b 2804 $dstat =~ s/$;//g;
c45dcabd
AW
2805 $dstat =~ s/\\\n.//g;
2806 $dstat =~ s/^\s*//s;
2807 $dstat =~ s/\s*$//s;
de7d4f0e 2808
c45dcabd 2809 # Flatten any parentheses and braces
bf30d6ed
AW
2810 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2811 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2812 $dstat =~ s/\[[^\{\}]*\]/1/)
2813 {
de7d4f0e 2814 }
d8aaf121 2815
c45dcabd
AW
2816 my $exceptions = qr{
2817 $Declare|
2818 module_param_named|
2819 MODULE_PARAM_DESC|
2820 DECLARE_PER_CPU|
2821 DEFINE_PER_CPU|
383099fd 2822 __typeof__\(|
22fd2d3e
SS
2823 union|
2824 struct|
ea71a0a0
AW
2825 \.$Ident\s*=\s*|
2826 ^\"|\"$
c45dcabd 2827 }x;
5eaa20b9 2828 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
f74bd194
AW
2829 if ($dstat ne '' &&
2830 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
2831 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
2832 $dstat !~ /^(?:$Ident|-?$Constant)$/ && # 10 // foo()
2833 $dstat !~ /$exceptions/ &&
2834 $dstat !~ /^\.$Ident\s*=/ && # .foo =
2835 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;$/ && # do {...} while (...);
2836 $dstat !~ /^for\s*$Constant$/ && # for (...)
2837 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
2838 $dstat !~ /^do\s*{/ && # do {...
2839 $dstat !~ /^\({/) # ({...
2840 {
2841 $ctx =~ s/\n*$//;
2842 my $herectx = $here . "\n";
2843 my $cnt = statement_rawlines($ctx);
2844
2845 for (my $n = 0; $n < $cnt; $n++) {
2846 $herectx .= raw_line($linenr, $n) . "\n";
c45dcabd
AW
2847 }
2848
f74bd194
AW
2849 if ($dstat =~ /;/) {
2850 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2851 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
2852 } else {
000d1cc1 2853 ERROR("COMPLEX_MACRO",
f74bd194 2854 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
d8aaf121 2855 }
653d4876 2856 }
0a920b5b
AW
2857 }
2858
080ba929
MF
2859# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2860# all assignments may have only one of the following with an assignment:
2861# .
2862# ALIGN(...)
2863# VMLINUX_SYMBOL(...)
2864 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
000d1cc1
JP
2865 WARN("MISSING_VMLINUX_SYMBOL",
2866 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
080ba929
MF
2867 }
2868
f0a594c1 2869# check for redundant bracing round if etc
13214adf
AW
2870 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2871 my ($level, $endln, @chunks) =
cf655043 2872 ctx_statement_full($linenr, $realcnt, 1);
13214adf 2873 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
cf655043
AW
2874 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2875 if ($#chunks > 0 && $level == 0) {
13214adf
AW
2876 my $allowed = 0;
2877 my $seen = 0;
773647a0 2878 my $herectx = $here . "\n";
cf655043 2879 my $ln = $linenr - 1;
13214adf
AW
2880 for my $chunk (@chunks) {
2881 my ($cond, $block) = @{$chunk};
2882
773647a0
AW
2883 # If the condition carries leading newlines, then count those as offsets.
2884 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2885 my $offset = statement_rawlines($whitespace) - 1;
2886
2887 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2888
2889 # We have looked at and allowed this specific line.
2890 $suppress_ifbraces{$ln + $offset} = 1;
2891
2892 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
cf655043
AW
2893 $ln += statement_rawlines($block) - 1;
2894
773647a0 2895 substr($block, 0, length($cond), '');
13214adf
AW
2896
2897 $seen++ if ($block =~ /^\s*{/);
2898
cf655043
AW
2899 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2900 if (statement_lines($cond) > 1) {
2901 #print "APW: ALLOWED: cond<$cond>\n";
13214adf
AW
2902 $allowed = 1;
2903 }
2904 if ($block =~/\b(?:if|for|while)\b/) {
cf655043 2905 #print "APW: ALLOWED: block<$block>\n";
13214adf
AW
2906 $allowed = 1;
2907 }
cf655043
AW
2908 if (statement_block_size($block) > 1) {
2909 #print "APW: ALLOWED: lines block<$block>\n";
13214adf
AW
2910 $allowed = 1;
2911 }
2912 }
2913 if ($seen && !$allowed) {
000d1cc1
JP
2914 WARN("BRACES",
2915 "braces {} are not necessary for any arm of this statement\n" . $herectx);
13214adf
AW
2916 }
2917 }
2918 }
773647a0 2919 if (!defined $suppress_ifbraces{$linenr - 1} &&
13214adf 2920 $line =~ /\b(if|while|for|else)\b/) {
cf655043
AW
2921 my $allowed = 0;
2922
2923 # Check the pre-context.
2924 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2925 #print "APW: ALLOWED: pre<$1>\n";
2926 $allowed = 1;
2927 }
773647a0
AW
2928
2929 my ($level, $endln, @chunks) =
2930 ctx_statement_full($linenr, $realcnt, $-[0]);
2931
cf655043
AW
2932 # Check the condition.
2933 my ($cond, $block) = @{$chunks[0]};
773647a0 2934 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
cf655043 2935 if (defined $cond) {
773647a0 2936 substr($block, 0, length($cond), '');
cf655043
AW
2937 }
2938 if (statement_lines($cond) > 1) {
2939 #print "APW: ALLOWED: cond<$cond>\n";
2940 $allowed = 1;
2941 }
2942 if ($block =~/\b(?:if|for|while)\b/) {
2943 #print "APW: ALLOWED: block<$block>\n";
2944 $allowed = 1;
2945 }
2946 if (statement_block_size($block) > 1) {
2947 #print "APW: ALLOWED: lines block<$block>\n";
2948 $allowed = 1;
2949 }
2950 # Check the post-context.
2951 if (defined $chunks[1]) {
2952 my ($cond, $block) = @{$chunks[1]};
2953 if (defined $cond) {
773647a0 2954 substr($block, 0, length($cond), '');
cf655043
AW
2955 }
2956 if ($block =~ /^\s*\{/) {
2957 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2958 $allowed = 1;
2959 }
2960 }
2961 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
69932487 2962 my $herectx = $here . "\n";
f055663c 2963 my $cnt = statement_rawlines($block);
cf655043 2964
f055663c 2965 for (my $n = 0; $n < $cnt; $n++) {
69932487 2966 $herectx .= raw_line($linenr, $n) . "\n";
f0a594c1 2967 }
cf655043 2968
000d1cc1
JP
2969 WARN("BRACES",
2970 "braces {} are not necessary for single statement blocks\n" . $herectx);
f0a594c1
AW
2971 }
2972 }
2973
653d4876 2974# don't include deprecated include files (uses RAW line)
4a0df2ef 2975 for my $inc (@dep_includes) {
c45dcabd 2976 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
000d1cc1
JP
2977 ERROR("DEPRECATED_INCLUDE",
2978 "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
0a920b5b
AW
2979 }
2980 }
2981
4a0df2ef
AW
2982# don't use deprecated functions
2983 for my $func (@dep_functions) {
00df344f 2984 if ($line =~ /\b$func\b/) {
000d1cc1
JP
2985 ERROR("DEPRECATED_FUNCTION",
2986 "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
4a0df2ef
AW
2987 }
2988 }
2989
2990# no volatiles please
6c72ffaa
AW
2991 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2992 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
000d1cc1
JP
2993 WARN("VOLATILE",
2994 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4a0df2ef
AW
2995 }
2996
00df344f 2997# warn about #if 0
c45dcabd 2998 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
000d1cc1
JP
2999 CHK("REDUNDANT_CODE",
3000 "if this code is redundant consider removing it\n" .
de7d4f0e 3001 $herecurr);
4a0df2ef
AW
3002 }
3003
f0a594c1
AW
3004# check for needless kfree() checks
3005 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3006 my $expr = $1;
3007 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
000d1cc1
JP
3008 WARN("NEEDLESS_KFREE",
3009 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
f0a594c1
AW
3010 }
3011 }
4c432a8f
GKH
3012# check for needless usb_free_urb() checks
3013 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3014 my $expr = $1;
3015 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
000d1cc1
JP
3016 WARN("NEEDLESS_USB_FREE_URB",
3017 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
4c432a8f
GKH
3018 }
3019 }
f0a594c1 3020
1a15a250
PP
3021# prefer usleep_range over udelay
3022 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3023 # ignore udelay's < 10, however
3024 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
000d1cc1
JP
3025 CHK("USLEEP_RANGE",
3026 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
1a15a250
PP
3027 }
3028 }
3029
09ef8725
PP
3030# warn about unexpectedly long msleep's
3031 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3032 if ($1 < 20) {
000d1cc1
JP
3033 WARN("MSLEEP",
3034 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
09ef8725
PP
3035 }
3036 }
3037
00df344f 3038# warn about #ifdefs in C files
c45dcabd 3039# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
00df344f
AW
3040# print "#ifdef in C files should be avoided\n";
3041# print "$herecurr";
3042# $clean = 0;
3043# }
3044
22f2a2ef 3045# warn about spacing in #ifdefs
c45dcabd 3046 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
000d1cc1
JP
3047 ERROR("SPACING",
3048 "exactly one space required after that #$1\n" . $herecurr);
22f2a2ef
AW
3049 }
3050
4a0df2ef 3051# check for spinlock_t definitions without a comment.
171ae1a4
AW
3052 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3053 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4a0df2ef
AW
3054 my $which = $1;
3055 if (!ctx_has_comment($first_line, $linenr)) {
000d1cc1
JP
3056 CHK("UNCOMMENTED_DEFINITION",
3057 "$1 definition without comment\n" . $herecurr);
4a0df2ef
AW
3058 }
3059 }
3060# check for memory barriers without a comment.
3061 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3062 if (!ctx_has_comment($first_line, $linenr)) {
000d1cc1
JP
3063 CHK("MEMORY_BARRIER",
3064 "memory barrier without comment\n" . $herecurr);
4a0df2ef
AW
3065 }
3066 }
3067# check of hardware specific defines
c45dcabd 3068 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
000d1cc1
JP
3069 CHK("ARCH_DEFINES",
3070 "architecture specific defines should be avoided\n" . $herecurr);
0a920b5b 3071 }
653d4876 3072
d4977c78
TK
3073# Check that the storage class is at the beginning of a declaration
3074 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
000d1cc1
JP
3075 WARN("STORAGE_CLASS",
3076 "storage class should be at the beginning of the declaration\n" . $herecurr)
d4977c78
TK
3077 }
3078
de7d4f0e
AW
3079# check the location of the inline attribute, that it is between
3080# storage class and type.
9c0ca6f9
AW
3081 if ($line =~ /\b$Type\s+$Inline\b/ ||
3082 $line =~ /\b$Inline\s+$Storage\b/) {
000d1cc1
JP
3083 ERROR("INLINE_LOCATION",
3084 "inline keyword should sit between storage class and type\n" . $herecurr);
de7d4f0e
AW
3085 }
3086
8905a67c
AW
3087# Check for __inline__ and __inline, prefer inline
3088 if ($line =~ /\b(__inline__|__inline)\b/) {
000d1cc1
JP
3089 WARN("INLINE",
3090 "plain inline is preferred over $1\n" . $herecurr);
8905a67c
AW
3091 }
3092
3d130fd0
JP
3093# Check for __attribute__ packed, prefer __packed
3094 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
000d1cc1
JP
3095 WARN("PREFER_PACKED",
3096 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3d130fd0
JP
3097 }
3098
39b7e287
JP
3099# Check for __attribute__ aligned, prefer __aligned
3100 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
000d1cc1
JP
3101 WARN("PREFER_ALIGNED",
3102 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
39b7e287
JP
3103 }
3104
5f14d3bd
JP
3105# Check for __attribute__ format(printf, prefer __printf
3106 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3107 WARN("PREFER_PRINTF",
3108 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3109 }
3110
8f53a9b8
JP
3111# check for sizeof(&)
3112 if ($line =~ /\bsizeof\s*\(\s*\&/) {
000d1cc1
JP
3113 WARN("SIZEOF_ADDRESS",
3114 "sizeof(& should be avoided\n" . $herecurr);
8f53a9b8
JP
3115 }
3116
428e2fdc
JP
3117# check for line continuations in quoted strings with odd counts of "
3118 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
000d1cc1
JP
3119 WARN("LINE_CONTINUATIONS",
3120 "Avoid line continuations in quoted strings\n" . $herecurr);
428e2fdc
JP
3121 }
3122
de7d4f0e 3123# check for new externs in .c files.
171ae1a4 3124 if ($realfile =~ /\.c$/ && defined $stat &&
c45dcabd 3125 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
171ae1a4 3126 {
c45dcabd
AW
3127 my $function_name = $1;
3128 my $paren_space = $2;
171ae1a4
AW
3129
3130 my $s = $stat;
3131 if (defined $cond) {
3132 substr($s, 0, length($cond), '');
3133 }
c45dcabd
AW
3134 if ($s =~ /^\s*;/ &&
3135 $function_name ne 'uninitialized_var')
3136 {
000d1cc1
JP
3137 WARN("AVOID_EXTERNS",
3138 "externs should be avoided in .c files\n" . $herecurr);
171ae1a4
AW
3139 }
3140
3141 if ($paren_space =~ /\n/) {
000d1cc1
JP
3142 WARN("FUNCTION_ARGUMENTS",
3143 "arguments for function declarations should follow identifier\n" . $herecurr);
171ae1a4 3144 }
9c9ba34e
AW
3145
3146 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3147 $stat =~ /^.\s*extern\s+/)
3148 {
000d1cc1
JP
3149 WARN("AVOID_EXTERNS",
3150 "externs should be avoided in .c files\n" . $herecurr);
de7d4f0e
AW
3151 }
3152
3153# checks for new __setup's
3154 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3155 my $name = $1;
3156
3157 if (!grep(/$name/, @setup_docs)) {
000d1cc1
JP
3158 CHK("UNDOCUMENTED_SETUP",
3159 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
de7d4f0e 3160 }
653d4876 3161 }
9c0ca6f9
AW
3162
3163# check for pointless casting of kmalloc return
caf2a54f 3164 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
000d1cc1
JP
3165 WARN("UNNECESSARY_CASTS",
3166 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
9c0ca6f9 3167 }
13214adf 3168
caf2a54f
JP
3169# check for multiple semicolons
3170 if ($line =~ /;\s*;\s*$/) {
000d1cc1
JP
3171 WARN("ONE_SEMICOLON",
3172 "Statements terminations use 1 semicolon\n" . $herecurr);
caf2a54f
JP
3173 }
3174
13214adf
AW
3175# check for gcc specific __FUNCTION__
3176 if ($line =~ /__FUNCTION__/) {
000d1cc1
JP
3177 WARN("USE_FUNC",
3178 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
13214adf 3179 }
773647a0 3180
4882720b
TG
3181# check for semaphores initialized locked
3182 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
000d1cc1
JP
3183 WARN("CONSIDER_COMPLETION",
3184 "consider using a completion\n" . $herecurr);
1704f47b 3185
773647a0 3186 }
67d0a075
JP
3187# recommend kstrto* over simple_strto* and strict_strto*
3188 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
000d1cc1 3189 WARN("CONSIDER_KSTRTO",
67d0a075 3190 "$1 is obsolete, use k$3 instead\n" . $herecurr);
773647a0 3191 }
f3db6639
ME
3192# check for __initcall(), use device_initcall() explicitly please
3193 if ($line =~ /^.\s*__initcall\s*\(/) {
000d1cc1
JP
3194 WARN("USE_DEVICE_INITCALL",
3195 "please use device_initcall() instead of __initcall()\n" . $herecurr);
f3db6639 3196 }
79404849
ER
3197# check for various ops structs, ensure they are const.
3198 my $struct_ops = qr{acpi_dock_ops|
3199 address_space_operations|
3200 backlight_ops|
3201 block_device_operations|
3202 dentry_operations|
3203 dev_pm_ops|
3204 dma_map_ops|
3205 extent_io_ops|
3206 file_lock_operations|
3207 file_operations|
3208 hv_ops|
3209 ide_dma_ops|
3210 intel_dvo_dev_ops|
3211 item_operations|
3212 iwl_ops|
3213 kgdb_arch|
3214 kgdb_io|
3215 kset_uevent_ops|
3216 lock_manager_operations|
3217 microcode_ops|
3218 mtrr_ops|
3219 neigh_ops|
3220 nlmsvc_binding|
3221 pci_raw_ops|
3222 pipe_buf_operations|
3223 platform_hibernation_ops|
3224 platform_suspend_ops|
3225 proto_ops|
3226 rpc_pipe_ops|
3227 seq_operations|
3228 snd_ac97_build_ops|
3229 soc_pcmcia_socket_ops|
3230 stacktrace_ops|
3231 sysfs_ops|
3232 tty_operations|
3233 usb_mon_operations|
3234 wd_ops}x;
6903ffb2 3235 if ($line !~ /\bconst\b/ &&
79404849 3236 $line =~ /\bstruct\s+($struct_ops)\b/) {
000d1cc1
JP
3237 WARN("CONST_STRUCT",
3238 "struct $1 should normally be const\n" .
6903ffb2 3239 $herecurr);
2b6db5cb 3240 }
773647a0
AW
3241
3242# use of NR_CPUS is usually wrong
3243# ignore definitions of NR_CPUS and usage to define arrays as likely right
3244 if ($line =~ /\bNR_CPUS\b/ &&
c45dcabd
AW
3245 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3246 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
171ae1a4
AW
3247 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3248 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3249 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
773647a0 3250 {
000d1cc1
JP
3251 WARN("NR_CPUS",
3252 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
773647a0 3253 }
9c9ba34e
AW
3254
3255# check for %L{u,d,i} in strings
3256 my $string;
3257 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3258 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2a1bc5d5 3259 $string =~ s/%%/__/g;
9c9ba34e 3260 if ($string =~ /(?<!%)%L[udi]/) {
000d1cc1
JP
3261 WARN("PRINTF_L",
3262 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
9c9ba34e
AW
3263 last;
3264 }
3265 }
691d77b6
AW
3266
3267# whine mightly about in_atomic
3268 if ($line =~ /\bin_atomic\s*\(/) {
3269 if ($realfile =~ m@^drivers/@) {
000d1cc1
JP
3270 ERROR("IN_ATOMIC",
3271 "do not use in_atomic in drivers\n" . $herecurr);
f4a87736 3272 } elsif ($realfile !~ m@^kernel/@) {
000d1cc1
JP
3273 WARN("IN_ATOMIC",
3274 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
691d77b6
AW
3275 }
3276 }
1704f47b
PZ
3277
3278# check for lockdep_set_novalidate_class
3279 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3280 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3281 if ($realfile !~ m@^kernel/lockdep@ &&
3282 $realfile !~ m@^include/linux/lockdep@ &&
3283 $realfile !~ m@^drivers/base/core@) {
000d1cc1
JP
3284 ERROR("LOCKDEP",
3285 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
1704f47b
PZ
3286 }
3287 }
88f8831c
DJ
3288
3289 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3290 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
000d1cc1
JP
3291 WARN("EXPORTED_WORLD_WRITABLE",
3292 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
88f8831c 3293 }
309c00c7
DJ
3294
3295 # Check for memset with swapped arguments
3296 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
000d1cc1
JP
3297 ERROR("MEMSET",
3298 "memset size is 3rd argument, not the second.\n" . $herecurr);
309c00c7 3299 }
13214adf
AW
3300 }
3301
3302 # If we have no input at all, then there is nothing to report on
3303 # so just keep quiet.
3304 if ($#rawlines == -1) {
3305 exit(0);
0a920b5b
AW
3306 }
3307
8905a67c
AW
3308 # In mailback mode only produce a report in the negative, for
3309 # things that appear to be patches.
3310 if ($mailback && ($clean == 1 || !$is_patch)) {
3311 exit(0);
3312 }
3313
3314 # This is not a patch, and we are are in 'no-patch' mode so
3315 # just keep quiet.
3316 if (!$chk_patch && !$is_patch) {
3317 exit(0);
3318 }
3319
3320 if (!$is_patch) {
000d1cc1
JP
3321 ERROR("NOT_UNIFIED_DIFF",
3322 "Does not appear to be a unified-diff format patch\n");
0a920b5b
AW
3323 }
3324 if ($is_patch && $chk_signoff && $signoff == 0) {
000d1cc1
JP
3325 ERROR("MISSING_SIGN_OFF",
3326 "Missing Signed-off-by: line(s)\n");
0a920b5b
AW
3327 }
3328
8905a67c 3329 print report_dump();
13214adf
AW
3330 if ($summary && !($clean == 1 && $quiet == 1)) {
3331 print "$filename " if ($summary_file);
8905a67c
AW
3332 print "total: $cnt_error errors, $cnt_warn warnings, " .
3333 (($check)? "$cnt_chk checks, " : "") .
3334 "$cnt_lines lines checked\n";
3335 print "\n" if ($quiet == 0);
f0a594c1 3336 }
8905a67c 3337
d2c0a235
AW
3338 if ($quiet == 0) {
3339 # If there were whitespace errors which cleanpatch can fix
3340 # then suggest that.
3341 if ($rpt_cleaners) {
3342 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3343 print " scripts/cleanfile\n\n";
b0781216 3344 $rpt_cleaners = 0;
d2c0a235
AW
3345 }
3346 }
3347
000d1cc1
JP
3348 if (keys %ignore_type) {
3349 print "NOTE: Ignored message types:";
3350 foreach my $ignore (sort keys %ignore_type) {
3351 print " $ignore";
3352 }
3353 print "\n";
3354 print "\n" if ($quiet == 0);
3355 }
3356
0a920b5b 3357 if ($clean == 1 && $quiet == 0) {
c2fdda0d 3358 print "$vname has no obvious style problems and is ready for submission.\n"
0a920b5b
AW
3359 }
3360 if ($clean == 0 && $quiet == 0) {
000d1cc1
JP
3361 print << "EOM";
3362$vname has style problems, please review.
3363
3364If any of these errors are false positives, please report
3365them to the maintainer, see CHECKPATCH in MAINTAINERS.
3366EOM
0a920b5b 3367 }
13214adf 3368
0a920b5b
AW
3369 return $clean;
3370}