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