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