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