tty: fix BKL related leak and crash
[deliverable/linux.git] / scripts / checkpatch.pl
CommitLineData
0a920b5b
AW
1#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
00df344f 3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
0a920b5b
AW
4# (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
5# Licensed under the terms of the GNU GPL License version 2
6
7use strict;
8
9my $P = $0;
00df344f 10$P =~ s@.*/@@g;
0a920b5b 11
9c9ba34e 12my $V = '0.18';
0a920b5b
AW
13
14use Getopt::Long qw(:config no_auto_abbrev);
15
16my $quiet = 0;
17my $tree = 1;
18my $chk_signoff = 1;
19my $chk_patch = 1;
653d4876 20my $tst_type = 0;
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,
13214adf 47 'test-type!' => \$tst_type,
773647a0 48 'test-only=s' => \$tst_only,
0a920b5b
AW
49) or exit;
50
51my $exit = 0;
52
53if ($#ARGV < 0) {
00df344f 54 print "usage: $P [options] patchfile\n";
0a920b5b 55 print "version: $V\n";
13214adf
AW
56 print "options: -q => quiet\n";
57 print " --no-tree => run without a kernel tree\n";
58 print " --terse => one line per report\n";
59 print " --emacs => emacs compile window format\n";
60 print " --file => check a source file\n";
61 print " --strict => enable more subjective tests\n";
62 print " --root => path to the kernel tree root\n";
63 print " --no-summary => suppress the per-file summary\n";
64 print " --summary-file => include the filename in summary\n";
0a920b5b
AW
65 exit(1);
66}
67
c2fdda0d
AW
68my $dbg_values = 0;
69my $dbg_possible = 0;
70for my $key (keys %debug) {
71 eval "\${dbg_$key} = '$debug{$key}';"
72}
73
8905a67c
AW
74if ($terse) {
75 $emacs = 1;
76 $quiet++;
77}
78
6c72ffaa
AW
79if ($tree) {
80 if (defined $root) {
81 if (!top_of_kernel_tree($root)) {
82 die "$P: $root: --root does not point at a valid tree\n";
83 }
84 } else {
85 if (top_of_kernel_tree('.')) {
86 $root = '.';
87 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
88 top_of_kernel_tree($1)) {
89 $root = $1;
90 }
91 }
92
93 if (!defined $root) {
94 print "Must be run from the top-level dir. of a kernel tree\n";
95 exit(2);
96 }
0a920b5b
AW
97}
98
6c72ffaa
AW
99my $emitted_corrupt = 0;
100
101our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
102our $Storage = qr{extern|static|asmlinkage};
103our $Sparse = qr{
104 __user|
105 __kernel|
106 __force|
107 __iomem|
108 __must_check|
109 __init_refok|
cf655043 110 __kprobes
6c72ffaa
AW
111 }x;
112our $Attribute = qr{
113 const|
114 __read_mostly|
115 __kprobes|
116 __(?:mem|cpu|dev|)(?:initdata|init)
117 }x;
118our $Inline = qr{inline|__always_inline|noinline};
6c72ffaa
AW
119our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
120our $Lval = qr{$Ident(?:$Member)*};
121
122our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
123our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
124our $Operators = qr{
125 <=|>=|==|!=|
126 =>|->|<<|>>|<|>|!|~|
c2fdda0d 127 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
6c72ffaa
AW
128 }x;
129
8905a67c
AW
130our $NonptrType;
131our $Type;
132our $Declare;
133
171ae1a4
AW
134our $UTF8 = qr {
135 [\x09\x0A\x0D\x20-\x7E] # ASCII
136 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
137 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
138 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
139 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
140 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
141 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
142 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
143}x;
144
8905a67c
AW
145our @typeList = (
146 qr{void},
147 qr{char},
148 qr{short},
149 qr{int},
150 qr{long},
151 qr{unsigned},
152 qr{float},
153 qr{double},
154 qr{bool},
155 qr{long\s+int},
156 qr{long\s+long},
157 qr{long\s+long\s+int},
158 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
159 qr{struct\s+$Ident},
160 qr{union\s+$Ident},
161 qr{enum\s+$Ident},
162 qr{${Ident}_t},
163 qr{${Ident}_handler},
164 qr{${Ident}_handler_fn},
165);
166
167sub build_types {
168 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
169 $NonptrType = qr{
170 \b
171 (?:const\s+)?
172 (?:unsigned\s+)?
cf655043
AW
173 (?:
174 $all|
175 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
176 )
8905a67c
AW
177 (?:\s+$Sparse|\s+const)*
178 \b
179 }x;
180 $Type = qr{
181 \b$NonptrType\b
182 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
c2fdda0d 183 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
8905a67c
AW
184 }x;
185 $Declare = qr{(?:$Storage\s+)?$Type};
186}
187build_types();
6c72ffaa
AW
188
189$chk_signoff = 0 if ($file);
190
4a0df2ef
AW
191my @dep_includes = ();
192my @dep_functions = ();
6c72ffaa
AW
193my $removal = "Documentation/feature-removal-schedule.txt";
194if ($tree && -f "$root/$removal") {
195 open(REMOVE, "<$root/$removal") ||
196 die "$P: $removal: open failed - $!\n";
0a920b5b 197 while (<REMOVE>) {
f0a594c1
AW
198 if (/^Check:\s+(.*\S)/) {
199 for my $entry (split(/[, ]+/, $1)) {
200 if ($entry =~ m@include/(.*)@) {
4a0df2ef 201 push(@dep_includes, $1);
4a0df2ef 202
f0a594c1
AW
203 } elsif ($entry !~ m@/@) {
204 push(@dep_functions, $entry);
205 }
4a0df2ef 206 }
0a920b5b
AW
207 }
208 }
209}
210
00df344f 211my @rawlines = ();
c2fdda0d
AW
212my @lines = ();
213my $vname;
6c72ffaa
AW
214for my $filename (@ARGV) {
215 if ($file) {
216 open(FILE, "diff -u /dev/null $filename|") ||
217 die "$P: $filename: diff failed - $!\n";
218 } else {
219 open(FILE, "<$filename") ||
220 die "$P: $filename: open failed - $!\n";
0a920b5b 221 }
c2fdda0d
AW
222 if ($filename eq '-') {
223 $vname = 'Your patch';
224 } else {
225 $vname = $filename;
226 }
6c72ffaa
AW
227 while (<FILE>) {
228 chomp;
229 push(@rawlines, $_);
230 }
231 close(FILE);
c2fdda0d 232 if (!process($filename)) {
6c72ffaa
AW
233 $exit = 1;
234 }
235 @rawlines = ();
13214adf 236 @lines = ();
0a920b5b
AW
237}
238
239exit($exit);
240
241sub top_of_kernel_tree {
6c72ffaa
AW
242 my ($root) = @_;
243
244 my @tree_check = (
245 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
246 "README", "Documentation", "arch", "include", "drivers",
247 "fs", "init", "ipc", "kernel", "lib", "scripts",
248 );
249
250 foreach my $check (@tree_check) {
251 if (! -e $root . '/' . $check) {
252 return 0;
253 }
0a920b5b 254 }
6c72ffaa 255 return 1;
0a920b5b
AW
256}
257
258sub expand_tabs {
259 my ($str) = @_;
260
261 my $res = '';
262 my $n = 0;
263 for my $c (split(//, $str)) {
264 if ($c eq "\t") {
265 $res .= ' ';
266 $n++;
267 for (; ($n % 8) != 0; $n++) {
268 $res .= ' ';
269 }
270 next;
271 }
272 $res .= $c;
273 $n++;
274 }
275
276 return $res;
277}
6c72ffaa 278sub copy_spacing {
773647a0 279 (my $res = shift) =~ tr/\t/ /c;
6c72ffaa
AW
280 return $res;
281}
0a920b5b 282
4a0df2ef
AW
283sub line_stats {
284 my ($line) = @_;
285
286 # Drop the diff line leader and expand tabs
287 $line =~ s/^.//;
288 $line = expand_tabs($line);
289
290 # Pick the indent from the front of the line.
291 my ($white) = ($line =~ /^(\s*)/);
292
293 return (length($line), length($white));
294}
295
773647a0
AW
296my $sanitise_quote = '';
297
298sub sanitise_line_reset {
299 my ($in_comment) = @_;
300
301 if ($in_comment) {
302 $sanitise_quote = '*/';
303 } else {
304 $sanitise_quote = '';
305 }
306}
00df344f
AW
307sub sanitise_line {
308 my ($line) = @_;
309
310 my $res = '';
311 my $l = '';
312
c2fdda0d 313 my $qlen = 0;
773647a0
AW
314 my $off = 0;
315 my $c;
00df344f 316
773647a0
AW
317 # Always copy over the diff marker.
318 $res = substr($line, 0, 1);
319
320 for ($off = 1; $off < length($line); $off++) {
321 $c = substr($line, $off, 1);
322
323 # Comments we are wacking completly including the begin
324 # and end, all to $;.
325 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
326 $sanitise_quote = '*/';
327
328 substr($res, $off, 2, "$;$;");
329 $off++;
330 next;
00df344f 331 }
773647a0
AW
332 if (substr($line, $off, 2) eq $sanitise_quote) {
333 $sanitise_quote = '';
334 substr($res, $off, 2, "$;$;");
335 $off++;
336 next;
c2fdda0d 337 }
773647a0
AW
338
339 # A \ in a string means ignore the next character.
340 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
341 $c eq "\\") {
342 substr($res, $off, 2, 'XX');
343 $off++;
344 next;
00df344f 345 }
773647a0
AW
346 # Regular quotes.
347 if ($c eq "'" || $c eq '"') {
348 if ($sanitise_quote eq '') {
349 $sanitise_quote = $c;
00df344f 350
773647a0
AW
351 substr($res, $off, 1, $c);
352 next;
353 } elsif ($sanitise_quote eq $c) {
354 $sanitise_quote = '';
355 }
356 }
00df344f 357
773647a0
AW
358 #print "SQ:$sanitise_quote\n";
359 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
360 substr($res, $off, 1, $;);
361 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
362 substr($res, $off, 1, 'X');
363 } else {
364 substr($res, $off, 1, $c);
365 }
c2fdda0d
AW
366 }
367
368 # The pathname on a #include may be surrounded by '<' and '>'.
369 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
370 my $clean = 'X' x length($1);
371 $res =~ s@\<.*\>@<$clean>@;
372
373 # The whole of a #error is a string.
374 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
375 my $clean = 'X' x length($1);
376 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
377 }
378
00df344f
AW
379 return $res;
380}
381
8905a67c
AW
382sub ctx_statement_block {
383 my ($linenr, $remain, $off) = @_;
384 my $line = $linenr - 1;
385 my $blk = '';
386 my $soff = $off;
387 my $coff = $off - 1;
773647a0 388 my $coff_set = 0;
8905a67c 389
13214adf
AW
390 my $loff = 0;
391
8905a67c
AW
392 my $type = '';
393 my $level = 0;
cf655043 394 my $p;
8905a67c
AW
395 my $c;
396 my $len = 0;
13214adf
AW
397
398 my $remainder;
8905a67c 399 while (1) {
773647a0 400 #warn "CSB: blk<$blk> remain<$remain>\n";
8905a67c
AW
401 # If we are about to drop off the end, pull in more
402 # context.
403 if ($off >= $len) {
404 for (; $remain > 0; $line++) {
c2fdda0d 405 next if ($lines[$line] =~ /^-/);
8905a67c 406 $remain--;
13214adf 407 $loff = $len;
c2fdda0d 408 $blk .= $lines[$line] . "\n";
8905a67c
AW
409 $len = length($blk);
410 $line++;
411 last;
412 }
413 # Bail if there is no further context.
414 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
13214adf 415 if ($off >= $len) {
8905a67c
AW
416 last;
417 }
418 }
cf655043 419 $p = $c;
8905a67c 420 $c = substr($blk, $off, 1);
13214adf 421 $remainder = substr($blk, $off);
8905a67c 422
773647a0 423 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
8905a67c
AW
424 # Statement ends at the ';' or a close '}' at the
425 # outermost level.
426 if ($level == 0 && $c eq ';') {
427 last;
428 }
429
13214adf 430 # An else is really a conditional as long as its not else if
773647a0
AW
431 if ($level == 0 && $coff_set == 0 &&
432 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
433 $remainder =~ /^(else)(?:\s|{)/ &&
434 $remainder !~ /^else\s+if\b/) {
435 $coff = $off + length($1) - 1;
436 $coff_set = 1;
437 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
438 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
13214adf
AW
439 }
440
8905a67c
AW
441 if (($type eq '' || $type eq '(') && $c eq '(') {
442 $level++;
443 $type = '(';
444 }
445 if ($type eq '(' && $c eq ')') {
446 $level--;
447 $type = ($level != 0)? '(' : '';
448
449 if ($level == 0 && $coff < $soff) {
450 $coff = $off;
773647a0
AW
451 $coff_set = 1;
452 #warn "CSB: mark coff<$coff>\n";
8905a67c
AW
453 }
454 }
455 if (($type eq '' || $type eq '{') && $c eq '{') {
456 $level++;
457 $type = '{';
458 }
459 if ($type eq '{' && $c eq '}') {
460 $level--;
461 $type = ($level != 0)? '{' : '';
462
463 if ($level == 0) {
464 last;
465 }
466 }
467 $off++;
468 }
13214adf
AW
469 if ($off == $len) {
470 $line++;
471 $remain--;
472 }
8905a67c
AW
473
474 my $statement = substr($blk, $soff, $off - $soff + 1);
475 my $condition = substr($blk, $soff, $coff - $soff + 1);
476
477 #warn "STATEMENT<$statement>\n";
478 #warn "CONDITION<$condition>\n";
479
773647a0 480 #print "coff<$coff> soff<$off> loff<$loff>\n";
13214adf
AW
481
482 return ($statement, $condition,
483 $line, $remain + 1, $off - $loff + 1, $level);
484}
485
cf655043
AW
486sub statement_lines {
487 my ($stmt) = @_;
488
489 # Strip the diff line prefixes and rip blank lines at start and end.
490 $stmt =~ s/(^|\n)./$1/g;
491 $stmt =~ s/^\s*//;
492 $stmt =~ s/\s*$//;
493
494 my @stmt_lines = ($stmt =~ /\n/g);
495
496 return $#stmt_lines + 2;
497}
498
499sub statement_rawlines {
500 my ($stmt) = @_;
501
502 my @stmt_lines = ($stmt =~ /\n/g);
503
504 return $#stmt_lines + 2;
505}
506
507sub statement_block_size {
508 my ($stmt) = @_;
509
510 $stmt =~ s/(^|\n)./$1/g;
511 $stmt =~ s/^\s*{//;
512 $stmt =~ s/}\s*$//;
513 $stmt =~ s/^\s*//;
514 $stmt =~ s/\s*$//;
515
516 my @stmt_lines = ($stmt =~ /\n/g);
517 my @stmt_statements = ($stmt =~ /;/g);
518
519 my $stmt_lines = $#stmt_lines + 2;
520 my $stmt_statements = $#stmt_statements + 1;
521
522 if ($stmt_lines > $stmt_statements) {
523 return $stmt_lines;
524 } else {
525 return $stmt_statements;
526 }
527}
528
13214adf
AW
529sub ctx_statement_full {
530 my ($linenr, $remain, $off) = @_;
531 my ($statement, $condition, $level);
532
533 my (@chunks);
534
cf655043 535 # Grab the first conditional/block pair.
13214adf
AW
536 ($statement, $condition, $linenr, $remain, $off, $level) =
537 ctx_statement_block($linenr, $remain, $off);
773647a0 538 #print "F: c<$condition> s<$statement> remain<$remain>\n";
cf655043
AW
539 push(@chunks, [ $condition, $statement ]);
540 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
541 return ($level, $linenr, @chunks);
542 }
543
544 # Pull in the following conditional/block pairs and see if they
545 # could continue the statement.
13214adf 546 for (;;) {
13214adf
AW
547 ($statement, $condition, $linenr, $remain, $off, $level) =
548 ctx_statement_block($linenr, $remain, $off);
cf655043 549 #print "C: c<$condition> s<$statement> remain<$remain>\n";
773647a0 550 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
cf655043
AW
551 #print "C: push\n";
552 push(@chunks, [ $condition, $statement ]);
13214adf
AW
553 }
554
555 return ($level, $linenr, @chunks);
8905a67c
AW
556}
557
4a0df2ef 558sub ctx_block_get {
f0a594c1 559 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
4a0df2ef
AW
560 my $line;
561 my $start = $linenr - 1;
4a0df2ef
AW
562 my $blk = '';
563 my @o;
564 my @c;
565 my @res = ();
566
f0a594c1 567 my $level = 0;
00df344f
AW
568 for ($line = $start; $remain > 0; $line++) {
569 next if ($rawlines[$line] =~ /^-/);
570 $remain--;
571
572 $blk .= $rawlines[$line];
f0a594c1
AW
573 foreach my $c (split(//, $rawlines[$line])) {
574 ##print "C<$c>L<$level><$open$close>O<$off>\n";
575 if ($off > 0) {
576 $off--;
577 next;
578 }
4a0df2ef 579
f0a594c1
AW
580 if ($c eq $close && $level > 0) {
581 $level--;
582 last if ($level == 0);
583 } elsif ($c eq $open) {
584 $level++;
585 }
586 }
4a0df2ef 587
f0a594c1 588 if (!$outer || $level <= 1) {
00df344f 589 push(@res, $rawlines[$line]);
4a0df2ef
AW
590 }
591
f0a594c1 592 last if ($level == 0);
4a0df2ef
AW
593 }
594
f0a594c1 595 return ($level, @res);
4a0df2ef
AW
596}
597sub ctx_block_outer {
598 my ($linenr, $remain) = @_;
599
f0a594c1
AW
600 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
601 return @r;
4a0df2ef
AW
602}
603sub ctx_block {
604 my ($linenr, $remain) = @_;
605
f0a594c1
AW
606 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
607 return @r;
653d4876
AW
608}
609sub ctx_statement {
f0a594c1
AW
610 my ($linenr, $remain, $off) = @_;
611
612 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
613 return @r;
614}
615sub ctx_block_level {
653d4876
AW
616 my ($linenr, $remain) = @_;
617
f0a594c1 618 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
4a0df2ef 619}
9c0ca6f9
AW
620sub ctx_statement_level {
621 my ($linenr, $remain, $off) = @_;
622
623 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
624}
4a0df2ef
AW
625
626sub ctx_locate_comment {
627 my ($first_line, $end_line) = @_;
628
629 # Catch a comment on the end of the line itself.
00df344f 630 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
4a0df2ef
AW
631 return $current_comment if (defined $current_comment);
632
633 # Look through the context and try and figure out if there is a
634 # comment.
635 my $in_comment = 0;
636 $current_comment = '';
637 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
00df344f
AW
638 my $line = $rawlines[$linenr - 1];
639 #warn " $line\n";
4a0df2ef
AW
640 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
641 $in_comment = 1;
642 }
643 if ($line =~ m@/\*@) {
644 $in_comment = 1;
645 }
646 if (!$in_comment && $current_comment ne '') {
647 $current_comment = '';
648 }
649 $current_comment .= $line . "\n" if ($in_comment);
650 if ($line =~ m@\*/@) {
651 $in_comment = 0;
652 }
653 }
654
655 chomp($current_comment);
656 return($current_comment);
657}
658sub ctx_has_comment {
659 my ($first_line, $end_line) = @_;
660 my $cmt = ctx_locate_comment($first_line, $end_line);
661
00df344f 662 ##print "LINE: $rawlines[$end_line - 1 ]\n";
4a0df2ef
AW
663 ##print "CMMT: $cmt\n";
664
665 return ($cmt ne '');
666}
667
6c72ffaa
AW
668sub cat_vet {
669 my ($vet) = @_;
670 my ($res, $coded);
9c0ca6f9 671
6c72ffaa
AW
672 $res = '';
673 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
674 $res .= $1;
675 if ($2 ne '') {
676 $coded = sprintf("^%c", unpack('C', $2) + 64);
677 $res .= $coded;
9c0ca6f9
AW
678 }
679 }
6c72ffaa 680 $res =~ s/$/\$/;
9c0ca6f9 681
6c72ffaa 682 return $res;
9c0ca6f9
AW
683}
684
c2fdda0d 685my $av_preprocessor = 0;
cf655043 686my $av_pending;
c2fdda0d
AW
687my @av_paren_type;
688
689sub annotate_reset {
690 $av_preprocessor = 0;
cf655043
AW
691 $av_pending = '_';
692 @av_paren_type = ('E');
c2fdda0d
AW
693}
694
6c72ffaa
AW
695sub annotate_values {
696 my ($stream, $type) = @_;
0a920b5b 697
6c72ffaa
AW
698 my $res;
699 my $cur = $stream;
700
c2fdda0d 701 print "$stream\n" if ($dbg_values > 1);
6c72ffaa 702
6c72ffaa 703 while (length($cur)) {
773647a0 704 @av_paren_type = ('E') if ($#av_paren_type < 0);
cf655043 705 print " <" . join('', @av_paren_type) .
171ae1a4 706 "> <$type> <$av_pending>" if ($dbg_values > 1);
6c72ffaa 707 if ($cur =~ /^(\s+)/o) {
c2fdda0d
AW
708 print "WS($1)\n" if ($dbg_values > 1);
709 if ($1 =~ /\n/ && $av_preprocessor) {
cf655043 710 $type = pop(@av_paren_type);
c2fdda0d 711 $av_preprocessor = 0;
6c72ffaa
AW
712 }
713
8905a67c 714 } elsif ($cur =~ /^($Type)/) {
c2fdda0d 715 print "DECLARE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
716 $type = 'T';
717
718 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
171ae1a4 719 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
c2fdda0d 720 $av_preprocessor = 1;
171ae1a4
AW
721 push(@av_paren_type, $type);
722 if ($2 ne '') {
723 $av_pending = 'N';
724 }
725 $type = 'E';
726
727 } elsif ($cur =~ /^(#\s*undef\s*$Ident)/o) {
728 print "UNDEF($1)\n" if ($dbg_values > 1);
729 $av_preprocessor = 1;
730 push(@av_paren_type, $type);
6c72ffaa 731
cf655043
AW
732 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
733 print "PRE_START($1)\n" if ($dbg_values > 1);
c2fdda0d 734 $av_preprocessor = 1;
cf655043
AW
735
736 push(@av_paren_type, $type);
737 push(@av_paren_type, $type);
171ae1a4 738 $type = 'E';
cf655043
AW
739
740 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
741 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
742 $av_preprocessor = 1;
743
744 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
745
171ae1a4 746 $type = 'E';
cf655043
AW
747
748 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
749 print "PRE_END($1)\n" if ($dbg_values > 1);
750
751 $av_preprocessor = 1;
752
753 # Assume all arms of the conditional end as this
754 # one does, and continue as if the #endif was not here.
755 pop(@av_paren_type);
756 push(@av_paren_type, $type);
171ae1a4 757 $type = 'E';
6c72ffaa
AW
758
759 } elsif ($cur =~ /^(\\\n)/o) {
c2fdda0d 760 print "PRECONT($1)\n" if ($dbg_values > 1);
6c72ffaa 761
171ae1a4
AW
762 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
763 print "ATTR($1)\n" if ($dbg_values > 1);
764 $av_pending = $type;
765 $type = 'N';
766
6c72ffaa 767 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
c2fdda0d 768 print "SIZEOF($1)\n" if ($dbg_values > 1);
6c72ffaa 769 if (defined $2) {
cf655043 770 $av_pending = 'V';
6c72ffaa
AW
771 }
772 $type = 'N';
773
13214adf 774 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
c2fdda0d 775 print "COND($1)\n" if ($dbg_values > 1);
cf655043 776 $av_pending = 'N';
6c72ffaa
AW
777 $type = 'N';
778
779 } elsif ($cur =~/^(return|case|else)/o) {
c2fdda0d 780 print "KEYWORD($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
781 $type = 'N';
782
783 } elsif ($cur =~ /^(\()/o) {
c2fdda0d 784 print "PAREN('$1')\n" if ($dbg_values > 1);
cf655043
AW
785 push(@av_paren_type, $av_pending);
786 $av_pending = '_';
6c72ffaa
AW
787 $type = 'N';
788
789 } elsif ($cur =~ /^(\))/o) {
cf655043
AW
790 my $new_type = pop(@av_paren_type);
791 if ($new_type ne '_') {
792 $type = $new_type;
c2fdda0d
AW
793 print "PAREN('$1') -> $type\n"
794 if ($dbg_values > 1);
6c72ffaa 795 } else {
c2fdda0d 796 print "PAREN('$1')\n" if ($dbg_values > 1);
6c72ffaa
AW
797 }
798
799 } elsif ($cur =~ /^($Ident)\(/o) {
c2fdda0d 800 print "FUNC($1)\n" if ($dbg_values > 1);
cf655043 801 $av_pending = 'V';
6c72ffaa
AW
802
803 } elsif ($cur =~ /^($Ident|$Constant)/o) {
c2fdda0d 804 print "IDENT($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
805 $type = 'V';
806
807 } elsif ($cur =~ /^($Assignment)/o) {
c2fdda0d 808 print "ASSIGN($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
809 $type = 'N';
810
cf655043 811 } elsif ($cur =~/^(;|{|})/) {
c2fdda0d 812 print "END($1)\n" if ($dbg_values > 1);
13214adf
AW
813 $type = 'E';
814
cf655043 815 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
13214adf 816 print "CLOSE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
817 $type = 'N';
818
819 } elsif ($cur =~ /^($Operators)/o) {
c2fdda0d 820 print "OP($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
821 if ($1 ne '++' && $1 ne '--') {
822 $type = 'N';
823 }
824
825 } elsif ($cur =~ /(^.)/o) {
c2fdda0d 826 print "C($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
827 }
828 if (defined $1) {
829 $cur = substr($cur, length($1));
830 $res .= $type x length($1);
831 }
9c0ca6f9 832 }
0a920b5b 833
9c0ca6f9 834 return $res;
0a920b5b
AW
835}
836
8905a67c 837sub possible {
13214adf 838 my ($possible, $line) = @_;
8905a67c
AW
839
840 #print "CHECK<$possible>\n";
841 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
842 $possible ne 'goto' && $possible ne 'return' &&
843 $possible ne 'struct' && $possible ne 'enum' &&
844 $possible ne 'case' && $possible ne 'else' &&
845 $possible ne 'typedef') {
13214adf 846 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
8905a67c
AW
847 push(@typeList, $possible);
848 build_types();
849 }
850}
851
6c72ffaa
AW
852my $prefix = '';
853
f0a594c1 854sub report {
773647a0
AW
855 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
856 return 0;
857 }
8905a67c
AW
858 my $line = $prefix . $_[0];
859
860 $line = (split('\n', $line))[0] . "\n" if ($terse);
861
13214adf 862 push(our @report, $line);
773647a0
AW
863
864 return 1;
f0a594c1
AW
865}
866sub report_dump {
13214adf 867 our @report;
f0a594c1 868}
de7d4f0e 869sub ERROR {
773647a0
AW
870 if (report("ERROR: $_[0]\n")) {
871 our $clean = 0;
872 our $cnt_error++;
873 }
de7d4f0e
AW
874}
875sub WARN {
773647a0
AW
876 if (report("WARNING: $_[0]\n")) {
877 our $clean = 0;
878 our $cnt_warn++;
879 }
de7d4f0e
AW
880}
881sub CHK {
773647a0 882 if ($check && report("CHECK: $_[0]\n")) {
6c72ffaa
AW
883 our $clean = 0;
884 our $cnt_chk++;
885 }
de7d4f0e
AW
886}
887
0a920b5b
AW
888sub process {
889 my $filename = shift;
0a920b5b
AW
890
891 my $linenr=0;
892 my $prevline="";
c2fdda0d 893 my $prevrawline="";
0a920b5b 894 my $stashline="";
c2fdda0d 895 my $stashrawline="";
0a920b5b 896
4a0df2ef 897 my $length;
0a920b5b
AW
898 my $indent;
899 my $previndent=0;
900 my $stashindent=0;
901
de7d4f0e 902 our $clean = 1;
0a920b5b
AW
903 my $signoff = 0;
904 my $is_patch = 0;
905
13214adf 906 our @report = ();
6c72ffaa
AW
907 our $cnt_lines = 0;
908 our $cnt_error = 0;
909 our $cnt_warn = 0;
910 our $cnt_chk = 0;
911
0a920b5b
AW
912 # Trace the real file/line as we go.
913 my $realfile = '';
914 my $realline = 0;
915 my $realcnt = 0;
916 my $here = '';
917 my $in_comment = 0;
c2fdda0d 918 my $comment_edge = 0;
0a920b5b
AW
919 my $first_line = 0;
920
13214adf
AW
921 my $prev_values = 'E';
922
923 # suppression flags
773647a0 924 my %suppress_ifbraces;
653d4876 925
c2fdda0d 926 # Pre-scan the patch sanitizing the lines.
de7d4f0e 927 # Pre-scan the patch looking for any __setup documentation.
c2fdda0d 928 #
de7d4f0e
AW
929 my @setup_docs = ();
930 my $setup_docs = 0;
773647a0
AW
931
932 sanitise_line_reset();
c2fdda0d
AW
933 my $line;
934 foreach my $rawline (@rawlines) {
773647a0
AW
935 $linenr++;
936 $line = $rawline;
c2fdda0d 937
773647a0 938 if ($rawline=~/^\+\+\+\s+(\S+)/) {
de7d4f0e
AW
939 $setup_docs = 0;
940 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
941 $setup_docs = 1;
942 }
773647a0
AW
943 #next;
944 }
945 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
946 $realline=$1-1;
947 if (defined $2) {
948 $realcnt=$3+1;
949 } else {
950 $realcnt=1+1;
951 }
952
953 # Guestimate if this is a continuing comment. Run
954 # the context looking for a comment "edge". If this
955 # edge is a close comment then we must be in a comment
956 # at context start.
957 my $edge;
171ae1a4 958 for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
773647a0
AW
959 next if ($line =~ /^-/);
960 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
961 last if (defined $edge);
962 }
963 if (defined $edge && $edge eq '*/') {
964 $in_comment = 1;
965 }
966
967 # Guestimate if this is a continuing comment. If this
968 # is the start of a diff block and this line starts
969 # ' *' then it is very likely a comment.
970 if (!defined $edge &&
971 $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
972 {
973 $in_comment = 1;
974 }
975
976 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
977 sanitise_line_reset($in_comment);
978
171ae1a4 979 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
773647a0 980 # Standardise the strings and chars within the input to
171ae1a4 981 # simplify matching -- only bother with positive lines.
773647a0 982 $line = sanitise_line($rawline);
de7d4f0e 983 }
773647a0
AW
984 push(@lines, $line);
985
986 if ($realcnt > 1) {
987 $realcnt-- if ($line =~ /^(?:\+| |$)/);
988 } else {
989 $realcnt = 0;
990 }
991
992 #print "==>$rawline\n";
993 #print "-->$line\n";
de7d4f0e
AW
994
995 if ($setup_docs && $line =~ /^\+/) {
996 push(@setup_docs, $line);
997 }
998 }
999
6c72ffaa
AW
1000 $prefix = '';
1001
773647a0
AW
1002 $realcnt = 0;
1003 $linenr = 0;
0a920b5b
AW
1004 foreach my $line (@lines) {
1005 $linenr++;
1006
c2fdda0d 1007 my $rawline = $rawlines[$linenr - 1];
6c72ffaa 1008
0a920b5b 1009#extract the line range in the file after the patch is applied
6c72ffaa 1010 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
0a920b5b 1011 $is_patch = 1;
4a0df2ef 1012 $first_line = $linenr + 1;
0a920b5b
AW
1013 $realline=$1-1;
1014 if (defined $2) {
1015 $realcnt=$3+1;
1016 } else {
1017 $realcnt=1+1;
1018 }
c2fdda0d 1019 annotate_reset();
13214adf
AW
1020 $prev_values = 'E';
1021
773647a0 1022 %suppress_ifbraces = ();
0a920b5b 1023 next;
0a920b5b 1024
4a0df2ef
AW
1025# track the line number as we move through the hunk, note that
1026# new versions of GNU diff omit the leading space on completely
1027# blank context lines so we need to count that too.
773647a0 1028 } elsif ($line =~ /^( |\+|$)/) {
0a920b5b 1029 $realline++;
d8aaf121 1030 $realcnt-- if ($realcnt != 0);
0a920b5b 1031
4a0df2ef 1032 # Measure the line length and indent.
c2fdda0d 1033 ($length, $indent) = line_stats($rawline);
0a920b5b
AW
1034
1035 # Track the previous line.
1036 ($prevline, $stashline) = ($stashline, $line);
1037 ($previndent, $stashindent) = ($stashindent, $indent);
c2fdda0d
AW
1038 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1039
773647a0 1040 #warn "line<$line>\n";
6c72ffaa 1041
d8aaf121
AW
1042 } elsif ($realcnt == 1) {
1043 $realcnt--;
0a920b5b
AW
1044 }
1045
1046#make up the handle for any error we report on this line
773647a0
AW
1047 $prefix = "$filename:$realline: " if ($emacs && $file);
1048 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1049
6c72ffaa
AW
1050 $here = "#$linenr: " if (!$file);
1051 $here = "#$realline: " if ($file);
773647a0
AW
1052
1053 # extract the filename as it passes
1054 if ($line=~/^\+\+\+\s+(\S+)/) {
1055 $realfile = $1;
1056 $realfile =~ s@^[^/]*/@@;
1057
1058 if ($realfile =~ m@include/asm/@) {
1059 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1060 }
1061 next;
1062 }
1063
389834b6 1064 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
0a920b5b 1065
c2fdda0d
AW
1066 my $hereline = "$here\n$rawline\n";
1067 my $herecurr = "$here\n$rawline\n";
1068 my $hereprev = "$here\n$prevrawline\n$rawline\n";
0a920b5b 1069
6c72ffaa
AW
1070 $cnt_lines++ if ($realcnt != 0);
1071
0a920b5b 1072#check the patch for a signoff:
d8aaf121 1073 if ($line =~ /^\s*signed-off-by:/i) {
4a0df2ef
AW
1074 # This is a signoff, if ugly, so do not double report.
1075 $signoff++;
0a920b5b 1076 if (!($line =~ /^\s*Signed-off-by:/)) {
de7d4f0e
AW
1077 WARN("Signed-off-by: is the preferred form\n" .
1078 $herecurr);
0a920b5b
AW
1079 }
1080 if ($line =~ /^\s*signed-off-by:\S/i) {
773647a0 1081 WARN("space required after Signed-off-by:\n" .
de7d4f0e 1082 $herecurr);
0a920b5b
AW
1083 }
1084 }
1085
00df344f 1086# Check for wrappage within a valid hunk of the file
8905a67c 1087 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
de7d4f0e 1088 ERROR("patch seems to be corrupt (line wrapped?)\n" .
6c72ffaa 1089 $herecurr) if (!$emitted_corrupt++);
de7d4f0e
AW
1090 }
1091
1092# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1093 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
171ae1a4
AW
1094 $rawline !~ m/^$UTF8*$/) {
1095 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1096
1097 my $blank = copy_spacing($rawline);
1098 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1099 my $hereptr = "$hereline$ptr\n";
1100
1101 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
00df344f
AW
1102 }
1103
1104#ignore lines being removed
1105 if ($line=~/^-/) {next;}
0a920b5b 1106
00df344f
AW
1107# check we are in a valid source file if not then ignore this hunk
1108 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
0a920b5b
AW
1109
1110#trailing whitespace
9c0ca6f9 1111 if ($line =~ /^\+.*\015/) {
c2fdda0d 1112 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
9c0ca6f9
AW
1113 ERROR("DOS line endings\n" . $herevet);
1114
c2fdda0d
AW
1115 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1116 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
de7d4f0e 1117 ERROR("trailing whitespace\n" . $herevet);
0a920b5b
AW
1118 }
1119#80 column limit
c2fdda0d 1120 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
de7d4f0e 1121 WARN("line over 80 characters\n" . $herecurr);
0a920b5b
AW
1122 }
1123
8905a67c
AW
1124# check for adding lines without a newline.
1125 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1126 WARN("adding a line without newline at end of file\n" . $herecurr);
1127 }
1128
0a920b5b
AW
1129# check we are in a valid source file *.[hc] if not then ignore this hunk
1130 next if ($realfile !~ /\.[hc]$/);
1131
1132# at the beginning of a line any tabs must come first and anything
1133# more than 8 must use tabs.
c2fdda0d
AW
1134 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1135 $rawline =~ /^\+\s* \s*/) {
1136 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
171ae1a4 1137 ERROR("code indent should use tabs where possible\n" . $herevet);
0a920b5b
AW
1138 }
1139
c2fdda0d 1140# check for RCS/CVS revision markers
cf655043 1141 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
c2fdda0d
AW
1142 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1143 }
22f2a2ef 1144
9c0ca6f9 1145# Check for potential 'bare' types
171ae1a4 1146 my ($stat, $cond);
9c9ba34e 1147 if ($realcnt && $line =~ /.\s*\S/) {
171ae1a4
AW
1148 ($stat, $cond) = ctx_statement_block($linenr,
1149 $realcnt, 0);
1150 $stat =~ s/\n./\n /g;
1151 $cond =~ s/\n./\n /g;
1152
1153 my $s = $stat;
1154 $s =~ s/{.*$//s;
cf655043 1155
c2fdda0d 1156 # Ignore goto labels.
171ae1a4 1157 if ($s =~ /$Ident:\*$/s) {
c2fdda0d
AW
1158
1159 # Ignore functions being called
171ae1a4 1160 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
c2fdda0d 1161
8905a67c 1162 # definitions in global scope can only start with types
171ae1a4 1163 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
cf655043 1164 possible($1, $s);
8905a67c
AW
1165
1166 # declarations always start with types
171ae1a4 1167 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/s) {
cf655043 1168 possible($1, $s);
c2fdda0d 1169 }
8905a67c
AW
1170
1171 # any (foo ... *) is a pointer cast, and foo is a type
171ae1a4 1172 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
cf655043 1173 possible($1, $s);
8905a67c
AW
1174 }
1175
1176 # Check for any sort of function declaration.
1177 # int foo(something bar, other baz);
1178 # void (*store_gdt)(x86_descr_ptr *);
171ae1a4 1179 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
8905a67c 1180 my ($name_len) = length($1);
8905a67c 1181
cf655043 1182 my $ctx = $s;
773647a0 1183 substr($ctx, 0, $name_len + 1, '');
8905a67c 1184 $ctx =~ s/\)[^\)]*$//;
cf655043 1185
8905a67c
AW
1186 for my $arg (split(/\s*,\s*/, $ctx)) {
1187 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1188
cf655043 1189 possible($1, $s);
8905a67c
AW
1190 }
1191 }
9c0ca6f9 1192 }
8905a67c 1193
9c0ca6f9
AW
1194 }
1195
653d4876
AW
1196#
1197# Checks which may be anchored in the context.
1198#
00df344f 1199
653d4876
AW
1200# Check for switch () and associated case and default
1201# statements should be at the same indent.
00df344f
AW
1202 if ($line=~/\bswitch\s*\(.*\)/) {
1203 my $err = '';
1204 my $sep = '';
1205 my @ctx = ctx_block_outer($linenr, $realcnt);
1206 shift(@ctx);
1207 for my $ctx (@ctx) {
1208 my ($clen, $cindent) = line_stats($ctx);
1209 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1210 $indent != $cindent) {
1211 $err .= "$sep$ctx\n";
1212 $sep = '';
1213 } else {
1214 $sep = "[...]\n";
1215 }
1216 }
1217 if ($err ne '') {
9c0ca6f9 1218 ERROR("switch and case should be at the same indent\n$hereline$err");
de7d4f0e
AW
1219 }
1220 }
1221
1222# if/while/etc brace do not go on next line, unless defining a do while loop,
1223# or if that brace on the next line is for something else
773647a0
AW
1224 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1225 my $pre_ctx = "$1$2";
1226
9c0ca6f9 1227 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
de7d4f0e
AW
1228 my $ctx_ln = $linenr + $#ctx + 1;
1229 my $ctx_cnt = $realcnt - $#ctx - 1;
1230 my $ctx = join("\n", @ctx);
1231
773647a0
AW
1232 ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1233
9c0ca6f9 1234 # Skip over any removed lines in the context following statement.
773647a0 1235 while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
de7d4f0e 1236 $ctx_ln++;
de7d4f0e 1237 }
773647a0 1238 ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
de7d4f0e 1239
773647a0
AW
1240 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1241 ERROR("that open brace { should be on the previous line\n" .
de7d4f0e 1242 "$here\n$ctx\n$lines[$ctx_ln - 1]");
00df344f 1243 }
773647a0
AW
1244 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1245 $ctx =~ /\)\s*\;\s*$/ &&
1246 defined $lines[$ctx_ln - 1])
1247 {
9c0ca6f9
AW
1248 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1249 if ($nindent > $indent) {
773647a0 1250 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
9c0ca6f9
AW
1251 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1252 }
1253 }
00df344f
AW
1254 }
1255
6c72ffaa
AW
1256 # Track the 'values' across context and added lines.
1257 my $opline = $line; $opline =~ s/^./ /;
1258 my $curr_values = annotate_values($opline . "\n", $prev_values);
1259 $curr_values = $prev_values . $curr_values;
c2fdda0d
AW
1260 if ($dbg_values) {
1261 my $outline = $opline; $outline =~ s/\t/ /g;
cf655043
AW
1262 print "$linenr > .$outline\n";
1263 print "$linenr > $curr_values\n";
c2fdda0d 1264 }
6c72ffaa
AW
1265 $prev_values = substr($curr_values, -1);
1266
00df344f
AW
1267#ignore lines not being added
1268 if ($line=~/^[^\+]/) {next;}
1269
653d4876
AW
1270# TEST: allow direct testing of the type matcher.
1271 if ($tst_type && $line =~ /^.$Declare$/) {
de7d4f0e 1272 ERROR("TEST: is type $Declare\n" . $herecurr);
653d4876
AW
1273 next;
1274 }
1275
f0a594c1
AW
1276# check for initialisation to aggregates open brace on the next line
1277 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1278 $line =~ /^.\s*{/) {
773647a0 1279 ERROR("that open brace { should be on the previous line\n" . $hereprev);
f0a594c1
AW
1280 }
1281
653d4876
AW
1282#
1283# Checks which are anchored on the added line.
1284#
1285
1286# check for malformed paths in #include statements (uses RAW line)
1287 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1288 my $path = $1;
1289 if ($path =~ m{//}) {
de7d4f0e
AW
1290 ERROR("malformed #include filename\n" .
1291 $herecurr);
653d4876 1292 }
653d4876 1293 }
00df344f 1294
0a920b5b 1295# no C99 // comments
00df344f 1296 if ($line =~ m{//}) {
de7d4f0e 1297 ERROR("do not use C99 // comments\n" . $herecurr);
0a920b5b 1298 }
00df344f 1299 # Remove C99 comments.
0a920b5b 1300 $line =~ s@//.*@@;
6c72ffaa 1301 $opline =~ s@//.*@@;
0a920b5b
AW
1302
1303#EXPORT_SYMBOL should immediately follow its function closing }.
653d4876
AW
1304 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1305 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1306 my $name = $1;
0a920b5b
AW
1307 if (($prevline !~ /^}/) &&
1308 ($prevline !~ /^\+}/) &&
653d4876 1309 ($prevline !~ /^ }/) &&
cf655043
AW
1310 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1311 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
171ae1a4 1312 ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
cf655043 1313 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
de7d4f0e 1314 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
0a920b5b
AW
1315 }
1316 }
1317
f0a594c1 1318# check for external initialisers.
9c9ba34e 1319 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL|false)\s*;/) {
f0a594c1
AW
1320 ERROR("do not initialise externals to 0 or NULL\n" .
1321 $herecurr);
1322 }
653d4876 1323# check for static initialisers.
9c9ba34e 1324 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
de7d4f0e
AW
1325 ERROR("do not initialise statics to 0 or NULL\n" .
1326 $herecurr);
0a920b5b
AW
1327 }
1328
653d4876
AW
1329# check for new typedefs, only function parameters and sparse annotations
1330# make sense.
1331 if ($line =~ /\btypedef\s/ &&
9c0ca6f9 1332 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
653d4876 1333 $line !~ /\b__bitwise(?:__|)\b/) {
de7d4f0e 1334 WARN("do not add new typedefs\n" . $herecurr);
0a920b5b
AW
1335 }
1336
1337# * goes on variable not on type
d8aaf121 1338 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
de7d4f0e
AW
1339 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1340 $herecurr);
d8aaf121
AW
1341
1342 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
de7d4f0e
AW
1343 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1344 $herecurr);
d8aaf121 1345
9c0ca6f9 1346 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
de7d4f0e
AW
1347 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1348 $herecurr);
d8aaf121 1349
9c0ca6f9 1350 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
de7d4f0e
AW
1351 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1352 $herecurr);
0a920b5b
AW
1353 }
1354
1355# # no BUG() or BUG_ON()
1356# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1357# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1358# print "$herecurr";
1359# $clean = 0;
1360# }
1361
8905a67c 1362 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
c2fdda0d 1363 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
8905a67c
AW
1364 }
1365
00df344f
AW
1366# printk should use KERN_* levels. Note that follow on printk's on the
1367# same line do not need a level, so we use the current block context
1368# to try and find and validate the current printk. In summary the current
1369# printk includes all preceeding printk's which have no newline on the end.
1370# we assume the first bad printk is the one to report.
f0a594c1 1371 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
00df344f
AW
1372 my $ok = 0;
1373 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1374 #print "CHECK<$lines[$ln - 1]\n";
1375 # we have a preceeding printk if it ends
1376 # with "\n" ignore it, else it is to blame
1377 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1378 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1379 $ok = 1;
1380 }
1381 last;
1382 }
1383 }
1384 if ($ok == 0) {
de7d4f0e 1385 WARN("printk() should include KERN_ facility level\n" . $herecurr);
00df344f 1386 }
0a920b5b
AW
1387 }
1388
653d4876
AW
1389# function brace can't be on same line, except for #defines of do while,
1390# or if closed on same line
c2fdda0d 1391 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
0a920b5b 1392 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
de7d4f0e 1393 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
0a920b5b 1394 }
653d4876 1395
8905a67c
AW
1396# open braces for enum, union and struct go on the same line.
1397 if ($line =~ /^.\s*{/ &&
1398 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1399 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1400 }
1401
f0a594c1 1402# check for spaces between functions and their parentheses.
6c72ffaa 1403 while ($line =~ /($Ident)\s+\(/g) {
c2fdda0d 1404 my $name = $1;
773647a0
AW
1405 my $ctx_before = substr($line, 0, $-[1]);
1406 my $ctx = "$ctx_before$name";
c2fdda0d
AW
1407
1408 # Ignore those directives where spaces _are_ permitted.
773647a0
AW
1409 if ($name =~ /^(?:
1410 if|for|while|switch|return|case|
1411 volatile|__volatile__|
1412 __attribute__|format|__extension__|
1413 asm|__asm__)$/x)
1414 {
c2fdda0d
AW
1415
1416 # cpp #define statements have non-optional spaces, ie
1417 # if there is a space between the name and the open
1418 # parenthesis it is simply not a parameter group.
773647a0
AW
1419 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1420
1421 # cpp #elif statement condition may start with a (
1422 } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
c2fdda0d
AW
1423
1424 # If this whole things ends with a type its most
1425 # likely a typedef for a function.
773647a0 1426 } elsif ($ctx =~ /$Type$/) {
c2fdda0d
AW
1427
1428 } else {
773647a0 1429 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
6c72ffaa 1430 }
f0a594c1 1431 }
653d4876 1432# Check operator spacing.
0a920b5b 1433 if (!($line=~/\#\s*include/)) {
9c0ca6f9
AW
1434 my $ops = qr{
1435 <<=|>>=|<=|>=|==|!=|
1436 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1437 =>|->|<<|>>|<|>|=|!|~|
c2fdda0d 1438 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
9c0ca6f9 1439 }x;
cf655043 1440 my @elements = split(/($ops|;)/, $opline);
00df344f 1441 my $off = 0;
6c72ffaa
AW
1442
1443 my $blank = copy_spacing($opline);
1444
0a920b5b 1445 for (my $n = 0; $n < $#elements; $n += 2) {
4a0df2ef
AW
1446 $off += length($elements[$n]);
1447
773647a0
AW
1448 # Pick up the preceeding and succeeding characters.
1449 my $ca = substr($opline, 0, $off);
1450 my $cc = '';
1451 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1452 $cc = substr($opline, $off + length($elements[$n + 1]));
1453 }
1454 my $cb = "$ca$;$cc";
1455
4a0df2ef
AW
1456 my $a = '';
1457 $a = 'V' if ($elements[$n] ne '');
1458 $a = 'W' if ($elements[$n] =~ /\s$/);
cf655043 1459 $a = 'C' if ($elements[$n] =~ /$;$/);
4a0df2ef
AW
1460 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1461 $a = 'O' if ($elements[$n] eq '');
773647a0 1462 $a = 'E' if ($ca =~ /^\s*$/);
4a0df2ef 1463
0a920b5b 1464 my $op = $elements[$n + 1];
4a0df2ef
AW
1465
1466 my $c = '';
0a920b5b 1467 if (defined $elements[$n + 2]) {
4a0df2ef
AW
1468 $c = 'V' if ($elements[$n + 2] ne '');
1469 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
cf655043 1470 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4a0df2ef
AW
1471 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1472 $c = 'O' if ($elements[$n + 2] eq '');
22f2a2ef 1473 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
4a0df2ef
AW
1474 } else {
1475 $c = 'E';
0a920b5b
AW
1476 }
1477
4a0df2ef
AW
1478 my $ctx = "${a}x${c}";
1479
1480 my $at = "(ctx:$ctx)";
1481
6c72ffaa 1482 my $ptr = substr($blank, 0, $off) . "^";
de7d4f0e 1483 my $hereptr = "$hereline$ptr\n";
0a920b5b 1484
9c0ca6f9
AW
1485 # Classify operators into binary, unary, or
1486 # definitions (* only) where they have more
1487 # than one mode.
6c72ffaa
AW
1488 my $op_type = substr($curr_values, $off + 1, 1);
1489 my $op_left = substr($curr_values, $off, 1);
1490 my $is_unary;
1491 if ($op_type eq 'T') {
1492 $is_unary = 2;
1493 } elsif ($op_left eq 'V') {
1494 $is_unary = 0;
1495 } else {
1496 $is_unary = 1;
9c0ca6f9 1497 }
9c0ca6f9 1498 #if ($op eq '-' || $op eq '&' || $op eq '*') {
6c72ffaa 1499 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
9c0ca6f9 1500 #}
0a920b5b 1501
13214adf
AW
1502 # Ignore operators passed as parameters.
1503 if ($op_type ne 'V' &&
1504 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1505
cf655043
AW
1506# # Ignore comments
1507# } elsif ($op =~ /^$;+$/) {
13214adf 1508
d8aaf121 1509 # ; should have either the end of line or a space or \ after it
13214adf 1510 } elsif ($op eq ';') {
cf655043
AW
1511 if ($ctx !~ /.x[WEBC]/ &&
1512 $cc !~ /^\\/ && $cc !~ /^;/) {
773647a0 1513 ERROR("space required after that '$op' $at\n" . $hereptr);
d8aaf121
AW
1514 }
1515
1516 # // is a comment
1517 } elsif ($op eq '//') {
0a920b5b
AW
1518
1519 # -> should have no spaces
1520 } elsif ($op eq '->') {
4a0df2ef 1521 if ($ctx =~ /Wx.|.xW/) {
773647a0 1522 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
0a920b5b
AW
1523 }
1524
1525 # , must have a space on the right.
1526 } elsif ($op eq ',') {
cf655043 1527 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
773647a0 1528 ERROR("space required after that '$op' $at\n" . $hereptr);
0a920b5b
AW
1529 }
1530
9c0ca6f9
AW
1531 # '*' as part of a type definition -- reported already.
1532 } elsif ($op eq '*' && $is_unary == 2) {
1533 #warn "'*' is part of type\n";
1534
1535 # unary operators should have a space before and
1536 # none after. May be left adjacent to another
1537 # unary operator, or a cast
1538 } elsif ($op eq '!' || $op eq '~' ||
1539 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
cf655043 1540 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
773647a0 1541 ERROR("space required before that '$op' $at\n" . $hereptr);
0a920b5b 1542 }
171ae1a4
AW
1543 if ($op eq '*' && $cc =~/\s*const\b/) {
1544 # A unary '*' may be const
1545
1546 } elsif ($ctx =~ /.xW/) {
773647a0 1547 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
0a920b5b
AW
1548 }
1549
1550 # unary ++ and unary -- are allowed no space on one side.
1551 } elsif ($op eq '++' or $op eq '--') {
773647a0
AW
1552 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1553 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1554 }
1555 if ($ctx =~ /Wx[BE]/ ||
1556 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1557 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
0a920b5b 1558 }
773647a0
AW
1559 if ($ctx =~ /ExW/) {
1560 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
653d4876 1561 }
0a920b5b 1562
773647a0 1563
0a920b5b 1564 # << and >> may either have or not have spaces both sides
9c0ca6f9
AW
1565 } elsif ($op eq '<<' or $op eq '>>' or
1566 $op eq '&' or $op eq '^' or $op eq '|' or
1567 $op eq '+' or $op eq '-' or
c2fdda0d
AW
1568 $op eq '*' or $op eq '/' or
1569 $op eq '%')
0a920b5b 1570 {
773647a0 1571 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
de7d4f0e
AW
1572 ERROR("need consistent spacing around '$op' $at\n" .
1573 $hereptr);
0a920b5b
AW
1574 }
1575
1576 # All the others need spaces both sides.
cf655043 1577 } elsif ($ctx !~ /[EWC]x[CWE]/) {
22f2a2ef
AW
1578 # Ignore email addresses <foo@bar>
1579 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1580 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
773647a0 1581 ERROR("spaces required around that '$op' $at\n" . $hereptr);
22f2a2ef 1582 }
0a920b5b 1583 }
4a0df2ef 1584 $off += length($elements[$n + 1]);
0a920b5b
AW
1585 }
1586 }
1587
f0a594c1
AW
1588# check for multiple assignments
1589 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
6c72ffaa 1590 CHK("multiple assignments should be avoided\n" . $herecurr);
f0a594c1
AW
1591 }
1592
22f2a2ef
AW
1593## # check for multiple declarations, allowing for a function declaration
1594## # continuation.
1595## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1596## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1597##
1598## # Remove any bracketed sections to ensure we do not
1599## # falsly report the parameters of functions.
1600## my $ln = $line;
1601## while ($ln =~ s/\([^\(\)]*\)//g) {
1602## }
1603## if ($ln =~ /,/) {
1604## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1605## }
1606## }
f0a594c1 1607
0a920b5b 1608#need space before brace following if, while, etc
22f2a2ef
AW
1609 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1610 $line =~ /do{/) {
773647a0 1611 ERROR("space required before the open brace '{'\n" . $herecurr);
de7d4f0e
AW
1612 }
1613
1614# closing brace should have a space following it when it has anything
1615# on the line
1616 if ($line =~ /}(?!(?:,|;|\)))\S/) {
773647a0 1617 ERROR("space required after that close brace '}'\n" . $herecurr);
0a920b5b
AW
1618 }
1619
22f2a2ef
AW
1620# check spacing on square brackets
1621 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
773647a0 1622 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
22f2a2ef
AW
1623 }
1624 if ($line =~ /\s\]/) {
773647a0 1625 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
22f2a2ef
AW
1626 }
1627
1628# check spacing on paretheses
9c0ca6f9
AW
1629 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1630 $line !~ /for\s*\(\s+;/) {
773647a0 1631 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
22f2a2ef 1632 }
13214adf 1633 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
9c0ca6f9 1634 $line !~ /for\s*\(.*;\s+\)/) {
773647a0 1635 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
22f2a2ef
AW
1636 }
1637
0a920b5b 1638#goto labels aren't indented, allow a single space however
4a0df2ef 1639 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
0a920b5b 1640 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
de7d4f0e 1641 WARN("labels should not be indented\n" . $herecurr);
0a920b5b
AW
1642 }
1643
1644# Need a space before open parenthesis after if, while etc
4a0df2ef 1645 if ($line=~/\b(if|while|for|switch)\(/) {
773647a0 1646 ERROR("space required before the open parenthesis '('\n" . $herecurr);
0a920b5b
AW
1647 }
1648
1649# Check for illegal assignment in if conditional.
8905a67c 1650 if ($line =~ /\bif\s*\(/) {
171ae1a4 1651 my ($s, $c) = ($stat, $cond);
8905a67c
AW
1652
1653 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
c2fdda0d 1654 ERROR("do not use assignment in if condition\n" . $herecurr);
8905a67c
AW
1655 }
1656
1657 # Find out what is on the end of the line after the
1658 # conditional.
773647a0 1659 substr($s, 0, length($c), '');
8905a67c 1660 $s =~ s/\n.*//g;
13214adf 1661 $s =~ s/$;//g; # Remove any comments
773647a0
AW
1662 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1663 $c !~ /^.\#\s*if/)
1664 {
8905a67c
AW
1665 ERROR("trailing statements should be on next line\n" . $herecurr);
1666 }
1667 }
1668
13214adf
AW
1669# Check for bitwise tests written as boolean
1670 if ($line =~ /
1671 (?:
1672 (?:\[|\(|\&\&|\|\|)
1673 \s*0[xX][0-9]+\s*
1674 (?:\&\&|\|\|)
1675 |
1676 (?:\&\&|\|\|)
1677 \s*0[xX][0-9]+\s*
1678 (?:\&\&|\|\||\)|\])
1679 )/x)
1680 {
1681 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1682 }
1683
8905a67c 1684# if and else should not have general statements after it
13214adf
AW
1685 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1686 my $s = $1;
1687 $s =~ s/$;//g; # Remove any comments
1688 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1689 ERROR("trailing statements should be on next line\n" . $herecurr);
1690 }
0a920b5b
AW
1691 }
1692
1693 # Check for }<nl>else {, these must be at the same
1694 # indent level to be relevant to each other.
1695 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1696 $previndent == $indent) {
de7d4f0e 1697 ERROR("else should follow close brace '}'\n" . $hereprev);
0a920b5b
AW
1698 }
1699
c2fdda0d
AW
1700 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1701 $previndent == $indent) {
1702 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1703
1704 # Find out what is on the end of the line after the
1705 # conditional.
773647a0 1706 substr($s, 0, length($c), '');
c2fdda0d
AW
1707 $s =~ s/\n.*//g;
1708
1709 if ($s =~ /^\s*;/) {
1710 ERROR("while should follow close brace '}'\n" . $hereprev);
1711 }
1712 }
1713
0a920b5b
AW
1714#studly caps, commented out until figure out how to distinguish between use of existing and adding new
1715# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1716# print "No studly caps, use _\n";
1717# print "$herecurr";
1718# $clean = 0;
1719# }
1720
1721#no spaces allowed after \ in define
1722 if ($line=~/\#define.*\\\s$/) {
de7d4f0e 1723 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
0a920b5b
AW
1724 }
1725
653d4876
AW
1726#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1727 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
6c72ffaa 1728 my $checkfile = "$root/include/linux/$1.h";
171ae1a4 1729 if (-f $checkfile && $1 ne 'irq') {
773647a0 1730 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
de7d4f0e 1731 $herecurr);
0a920b5b
AW
1732 }
1733 }
1734
653d4876
AW
1735# multi-statement macros should be enclosed in a do while loop, grab the
1736# first statement and ensure its the whole macro if its not enclosed
cf655043 1737# in a known good container
9c0ca6f9
AW
1738 if ($prevline =~ /\#define.*\\/ &&
1739 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1740 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1741 $line !~ /^.\s*$Declare\s/) {
653d4876
AW
1742 # Grab the first statement, if that is the entire macro
1743 # its ok. This may start either on the #define line
1744 # or the one below.
d8aaf121
AW
1745 my $ln = $linenr;
1746 my $cnt = $realcnt;
f0a594c1 1747 my $off = 0;
653d4876 1748
f0a594c1
AW
1749 # If the macro starts on the define line start
1750 # grabbing the statement after the identifier
1751 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1752 ##print "1<$1> 2<$2>\n";
22f2a2ef 1753 if (defined $2 && $2 ne '') {
f0a594c1 1754 $off = length($1);
d8aaf121
AW
1755 $ln--;
1756 $cnt++;
8905a67c
AW
1757 while ($lines[$ln - 1] =~ /^-/) {
1758 $ln--;
1759 $cnt++;
1760 }
d8aaf121 1761 }
f0a594c1 1762 my @ctx = ctx_statement($ln, $cnt, $off);
de7d4f0e
AW
1763 my $ctx_ln = $ln + $#ctx + 1;
1764 my $ctx = join("\n", @ctx);
1765
1766 # Pull in any empty extension lines.
1767 while ($ctx =~ /\\$/ &&
1768 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1769 $ctx .= $lines[$ctx_ln - 1];
1770 $ctx_ln++;
1771 }
d8aaf121
AW
1772
1773 if ($ctx =~ /\\$/) {
1774 if ($ctx =~ /;/) {
de7d4f0e 1775 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
d8aaf121 1776 } else {
de7d4f0e 1777 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
d8aaf121 1778 }
653d4876 1779 }
0a920b5b
AW
1780 }
1781
f0a594c1 1782# check for redundant bracing round if etc
13214adf
AW
1783 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1784 my ($level, $endln, @chunks) =
cf655043 1785 ctx_statement_full($linenr, $realcnt, 1);
13214adf 1786 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
cf655043
AW
1787 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1788 if ($#chunks > 0 && $level == 0) {
13214adf
AW
1789 my $allowed = 0;
1790 my $seen = 0;
773647a0 1791 my $herectx = $here . "\n";
cf655043 1792 my $ln = $linenr - 1;
13214adf
AW
1793 for my $chunk (@chunks) {
1794 my ($cond, $block) = @{$chunk};
1795
773647a0
AW
1796 # If the condition carries leading newlines, then count those as offsets.
1797 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1798 my $offset = statement_rawlines($whitespace) - 1;
1799
1800 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1801
1802 # We have looked at and allowed this specific line.
1803 $suppress_ifbraces{$ln + $offset} = 1;
1804
1805 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
cf655043
AW
1806 $ln += statement_rawlines($block) - 1;
1807
773647a0 1808 substr($block, 0, length($cond), '');
13214adf
AW
1809
1810 $seen++ if ($block =~ /^\s*{/);
1811
cf655043
AW
1812 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1813 if (statement_lines($cond) > 1) {
1814 #print "APW: ALLOWED: cond<$cond>\n";
13214adf
AW
1815 $allowed = 1;
1816 }
1817 if ($block =~/\b(?:if|for|while)\b/) {
cf655043 1818 #print "APW: ALLOWED: block<$block>\n";
13214adf
AW
1819 $allowed = 1;
1820 }
cf655043
AW
1821 if (statement_block_size($block) > 1) {
1822 #print "APW: ALLOWED: lines block<$block>\n";
13214adf
AW
1823 $allowed = 1;
1824 }
1825 }
1826 if ($seen && !$allowed) {
cf655043 1827 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
13214adf
AW
1828 }
1829 }
1830 }
773647a0 1831 if (!defined $suppress_ifbraces{$linenr - 1} &&
13214adf 1832 $line =~ /\b(if|while|for|else)\b/) {
cf655043
AW
1833 my $allowed = 0;
1834
1835 # Check the pre-context.
1836 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1837 #print "APW: ALLOWED: pre<$1>\n";
1838 $allowed = 1;
1839 }
773647a0
AW
1840
1841 my ($level, $endln, @chunks) =
1842 ctx_statement_full($linenr, $realcnt, $-[0]);
1843
cf655043
AW
1844 # Check the condition.
1845 my ($cond, $block) = @{$chunks[0]};
773647a0 1846 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
cf655043 1847 if (defined $cond) {
773647a0 1848 substr($block, 0, length($cond), '');
cf655043
AW
1849 }
1850 if (statement_lines($cond) > 1) {
1851 #print "APW: ALLOWED: cond<$cond>\n";
1852 $allowed = 1;
1853 }
1854 if ($block =~/\b(?:if|for|while)\b/) {
1855 #print "APW: ALLOWED: block<$block>\n";
1856 $allowed = 1;
1857 }
1858 if (statement_block_size($block) > 1) {
1859 #print "APW: ALLOWED: lines block<$block>\n";
1860 $allowed = 1;
1861 }
1862 # Check the post-context.
1863 if (defined $chunks[1]) {
1864 my ($cond, $block) = @{$chunks[1]};
1865 if (defined $cond) {
773647a0 1866 substr($block, 0, length($cond), '');
cf655043
AW
1867 }
1868 if ($block =~ /^\s*\{/) {
1869 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1870 $allowed = 1;
1871 }
1872 }
1873 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1874 my $herectx = $here . "\n";;
1875 my $end = $linenr + statement_rawlines($block) - 1;
1876
1877 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1878 $herectx .= $rawlines[$ln] . "\n";;
f0a594c1 1879 }
cf655043
AW
1880
1881 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
f0a594c1
AW
1882 }
1883 }
1884
653d4876 1885# don't include deprecated include files (uses RAW line)
4a0df2ef 1886 for my $inc (@dep_includes) {
653d4876 1887 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
de7d4f0e 1888 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
0a920b5b
AW
1889 }
1890 }
1891
4a0df2ef
AW
1892# don't use deprecated functions
1893 for my $func (@dep_functions) {
00df344f 1894 if ($line =~ /\b$func\b/) {
de7d4f0e 1895 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
4a0df2ef
AW
1896 }
1897 }
1898
1899# no volatiles please
6c72ffaa
AW
1900 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1901 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
de7d4f0e 1902 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4a0df2ef
AW
1903 }
1904
9c0ca6f9
AW
1905# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1906 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1907 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1908 }
1909
00df344f
AW
1910# warn about #if 0
1911 if ($line =~ /^.#\s*if\s+0\b/) {
de7d4f0e
AW
1912 CHK("if this code is redundant consider removing it\n" .
1913 $herecurr);
4a0df2ef
AW
1914 }
1915
f0a594c1
AW
1916# check for needless kfree() checks
1917 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1918 my $expr = $1;
1919 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1920 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1921 }
1922 }
c2010a3b
GKH
1923# check for needless usb_free_urb() checks
1924 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1925 my $expr = $1;
1926 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1927 WARN("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1928 }
1929 }
f0a594c1 1930
00df344f
AW
1931# warn about #ifdefs in C files
1932# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1933# print "#ifdef in C files should be avoided\n";
1934# print "$herecurr";
1935# $clean = 0;
1936# }
1937
22f2a2ef
AW
1938# warn about spacing in #ifdefs
1939 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1940 ERROR("exactly one space required after that #$1\n" . $herecurr);
1941 }
1942
4a0df2ef 1943# check for spinlock_t definitions without a comment.
171ae1a4
AW
1944 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
1945 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4a0df2ef
AW
1946 my $which = $1;
1947 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 1948 CHK("$1 definition without comment\n" . $herecurr);
4a0df2ef
AW
1949 }
1950 }
1951# check for memory barriers without a comment.
1952 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1953 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 1954 CHK("memory barrier without comment\n" . $herecurr);
4a0df2ef
AW
1955 }
1956 }
1957# check of hardware specific defines
22f2a2ef 1958 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
de7d4f0e 1959 CHK("architecture specific defines should be avoided\n" . $herecurr);
0a920b5b 1960 }
653d4876 1961
de7d4f0e
AW
1962# check the location of the inline attribute, that it is between
1963# storage class and type.
9c0ca6f9
AW
1964 if ($line =~ /\b$Type\s+$Inline\b/ ||
1965 $line =~ /\b$Inline\s+$Storage\b/) {
de7d4f0e
AW
1966 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1967 }
1968
8905a67c
AW
1969# Check for __inline__ and __inline, prefer inline
1970 if ($line =~ /\b(__inline__|__inline)\b/) {
1971 WARN("plain inline is preferred over $1\n" . $herecurr);
1972 }
1973
de7d4f0e 1974# check for new externs in .c files.
171ae1a4 1975 if ($realfile =~ /\.c$/ && defined $stat &&
9c9ba34e 1976 $stat =~ /^.\s*(?:extern\s+)?$Type\s+$Ident(\s*)\(/s)
171ae1a4
AW
1977 {
1978 my $paren_space = $1;
1979
1980 my $s = $stat;
1981 if (defined $cond) {
1982 substr($s, 0, length($cond), '');
1983 }
1984 if ($s =~ /^\s*;/) {
1985 WARN("externs should be avoided in .c files\n" . $herecurr);
1986 }
1987
1988 if ($paren_space =~ /\n/) {
1989 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
1990 }
9c9ba34e
AW
1991
1992 } elsif ($realfile =~ /\.c$/ && defined $stat &&
1993 $stat =~ /^.\s*extern\s+/)
1994 {
1995 WARN("externs should be avoided in .c files\n" . $herecurr);
de7d4f0e
AW
1996 }
1997
1998# checks for new __setup's
1999 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2000 my $name = $1;
2001
2002 if (!grep(/$name/, @setup_docs)) {
2003 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2004 }
653d4876 2005 }
9c0ca6f9
AW
2006
2007# check for pointless casting of kmalloc return
2008 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2009 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2010 }
13214adf
AW
2011
2012# check for gcc specific __FUNCTION__
2013 if ($line =~ /__FUNCTION__/) {
2014 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2015 }
773647a0
AW
2016
2017# check for semaphores used as mutexes
171ae1a4 2018 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
773647a0
AW
2019 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2020 }
2021# check for semaphores used as mutexes
171ae1a4 2022 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
773647a0
AW
2023 WARN("consider using a completion\n" . $herecurr);
2024 }
2025# recommend strict_strto* over simple_strto*
2026 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2027 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2028 }
2029
2030# use of NR_CPUS is usually wrong
2031# ignore definitions of NR_CPUS and usage to define arrays as likely right
2032 if ($line =~ /\bNR_CPUS\b/ &&
171ae1a4
AW
2033 $line !~ /^.#\s*if\b.*\bNR_CPUS\b/ &&
2034 $line !~ /^.#\s*define\b.*\bNR_CPUS\b/ &&
2035 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2036 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2037 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
773647a0
AW
2038 {
2039 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2040 }
9c9ba34e
AW
2041
2042# check for %L{u,d,i} in strings
2043 my $string;
2044 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2045 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2046 if ($string =~ /(?<!%)%L[udi]/) {
2047 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2048 last;
2049 }
2050 }
13214adf
AW
2051 }
2052
2053 # If we have no input at all, then there is nothing to report on
2054 # so just keep quiet.
2055 if ($#rawlines == -1) {
2056 exit(0);
0a920b5b
AW
2057 }
2058
8905a67c
AW
2059 # In mailback mode only produce a report in the negative, for
2060 # things that appear to be patches.
2061 if ($mailback && ($clean == 1 || !$is_patch)) {
2062 exit(0);
2063 }
2064
2065 # This is not a patch, and we are are in 'no-patch' mode so
2066 # just keep quiet.
2067 if (!$chk_patch && !$is_patch) {
2068 exit(0);
2069 }
2070
2071 if (!$is_patch) {
de7d4f0e 2072 ERROR("Does not appear to be a unified-diff format patch\n");
0a920b5b
AW
2073 }
2074 if ($is_patch && $chk_signoff && $signoff == 0) {
de7d4f0e 2075 ERROR("Missing Signed-off-by: line(s)\n");
0a920b5b
AW
2076 }
2077
8905a67c 2078 print report_dump();
13214adf
AW
2079 if ($summary && !($clean == 1 && $quiet == 1)) {
2080 print "$filename " if ($summary_file);
8905a67c
AW
2081 print "total: $cnt_error errors, $cnt_warn warnings, " .
2082 (($check)? "$cnt_chk checks, " : "") .
2083 "$cnt_lines lines checked\n";
2084 print "\n" if ($quiet == 0);
f0a594c1 2085 }
8905a67c 2086
0a920b5b 2087 if ($clean == 1 && $quiet == 0) {
c2fdda0d 2088 print "$vname has no obvious style problems and is ready for submission.\n"
0a920b5b
AW
2089 }
2090 if ($clean == 0 && $quiet == 0) {
c2fdda0d 2091 print "$vname has style problems, please review. If any of these errors\n";
0a920b5b
AW
2092 print "are false positives report them to the maintainer, see\n";
2093 print "CHECKPATCH in MAINTAINERS.\n";
2094 }
13214adf 2095
0a920b5b
AW
2096 return $clean;
2097}
This page took 0.227492 seconds and 5 git commands to generate.