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