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