[CVE-2009-0029] System call wrappers part 20
[linux/fpc-iii.git] / scripts / checkpatch.pl
blob7bed4ed2c5197c5d360533665dcea69bc6446109
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. <davej@redhat.com> (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008, Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
8 use strict;
10 my $P = $0;
11 $P =~ s@.*/@@g;
13 my $V = '0.26';
15 use Getopt::Long qw(:config no_auto_abbrev);
17 my $quiet = 0;
18 my $tree = 1;
19 my $chk_signoff = 1;
20 my $chk_patch = 1;
21 my $tst_only;
22 my $emacs = 0;
23 my $terse = 0;
24 my $file = 0;
25 my $check = 0;
26 my $summary = 1;
27 my $mailback = 0;
28 my $summary_file = 0;
29 my $root;
30 my %debug;
31 GetOptions(
32 'q|quiet+' => \$quiet,
33 'tree!' => \$tree,
34 'signoff!' => \$chk_signoff,
35 'patch!' => \$chk_patch,
36 'emacs!' => \$emacs,
37 'terse!' => \$terse,
38 'file!' => \$file,
39 'subjective!' => \$check,
40 'strict!' => \$check,
41 'root=s' => \$root,
42 'summary!' => \$summary,
43 'mailback!' => \$mailback,
44 'summary-file!' => \$summary_file,
46 'debug=s' => \%debug,
47 'test-only=s' => \$tst_only,
48 ) or exit;
50 my $exit = 0;
52 if ($#ARGV < 0) {
53 print "usage: $P [options] patchfile\n";
54 print "version: $V\n";
55 print "options: -q => quiet\n";
56 print " --no-tree => run without a kernel tree\n";
57 print " --terse => one line per report\n";
58 print " --emacs => emacs compile window format\n";
59 print " --file => check a source file\n";
60 print " --strict => enable more subjective tests\n";
61 print " --root => path to the kernel tree root\n";
62 print " --no-summary => suppress the per-file summary\n";
63 print " --summary-file => include the filename in summary\n";
64 exit(1);
67 my $dbg_values = 0;
68 my $dbg_possible = 0;
69 my $dbg_type = 0;
70 my $dbg_attr = 0;
71 for my $key (keys %debug) {
72 ## no critic
73 eval "\${dbg_$key} = '$debug{$key}';";
74 die "$@" if ($@);
77 if ($terse) {
78 $emacs = 1;
79 $quiet++;
82 if ($tree) {
83 if (defined $root) {
84 if (!top_of_kernel_tree($root)) {
85 die "$P: $root: --root does not point at a valid tree\n";
87 } else {
88 if (top_of_kernel_tree('.')) {
89 $root = '.';
90 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
91 top_of_kernel_tree($1)) {
92 $root = $1;
96 if (!defined $root) {
97 print "Must be run from the top-level dir. of a kernel tree\n";
98 exit(2);
102 my $emitted_corrupt = 0;
104 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
105 our $Storage = qr{extern|static|asmlinkage};
106 our $Sparse = qr{
107 __user|
108 __kernel|
109 __force|
110 __iomem|
111 __must_check|
112 __init_refok|
113 __kprobes
115 our $Attribute = qr{
116 const|
117 __read_mostly|
118 __kprobes|
119 __(?:mem|cpu|dev|)(?:initdata|init)|
120 ____cacheline_aligned|
121 ____cacheline_aligned_in_smp|
122 ____cacheline_internodealigned_in_smp|
123 __weak
125 our $Modifier;
126 our $Inline = qr{inline|__always_inline|noinline};
127 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
128 our $Lval = qr{$Ident(?:$Member)*};
130 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
131 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
132 our $Compare = qr{<=|>=|==|!=|<|>};
133 our $Operators = qr{
134 <=|>=|==|!=|
135 =>|->|<<|>>|<|>|!|~|
136 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
139 our $NonptrType;
140 our $Type;
141 our $Declare;
143 our $UTF8 = qr {
144 [\x09\x0A\x0D\x20-\x7E] # ASCII
145 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
146 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
147 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
148 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
149 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
150 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
151 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
154 our $typeTypedefs = qr{(?x:
155 (?:__)?(?:u|s|be|le)(?:\d|\d\d)|
156 atomic_t
159 our @typeList = (
160 qr{void},
161 qr{(?:unsigned\s+)?char},
162 qr{(?:unsigned\s+)?short},
163 qr{(?:unsigned\s+)?int},
164 qr{(?:unsigned\s+)?long},
165 qr{(?:unsigned\s+)?long\s+int},
166 qr{(?:unsigned\s+)?long\s+long},
167 qr{(?:unsigned\s+)?long\s+long\s+int},
168 qr{unsigned},
169 qr{float},
170 qr{double},
171 qr{bool},
172 qr{struct\s+$Ident},
173 qr{union\s+$Ident},
174 qr{enum\s+$Ident},
175 qr{${Ident}_t},
176 qr{${Ident}_handler},
177 qr{${Ident}_handler_fn},
179 our @modifierList = (
180 qr{fastcall},
183 sub build_types {
184 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
185 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
186 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
187 $NonptrType = qr{
188 (?:$Modifier\s+|const\s+)*
190 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
191 (?:$typeTypedefs\b)|
192 (?:${all}\b)
194 (?:\s+$Modifier|\s+const)*
196 $Type = qr{
197 $NonptrType
198 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
199 (?:\s+$Inline|\s+$Modifier)*
201 $Declare = qr{(?:$Storage\s+)?$Type};
203 build_types();
205 $chk_signoff = 0 if ($file);
207 my @dep_includes = ();
208 my @dep_functions = ();
209 my $removal = "Documentation/feature-removal-schedule.txt";
210 if ($tree && -f "$root/$removal") {
211 open(my $REMOVE, '<', "$root/$removal") ||
212 die "$P: $removal: open failed - $!\n";
213 while (<$REMOVE>) {
214 if (/^Check:\s+(.*\S)/) {
215 for my $entry (split(/[, ]+/, $1)) {
216 if ($entry =~ m@include/(.*)@) {
217 push(@dep_includes, $1);
219 } elsif ($entry !~ m@/@) {
220 push(@dep_functions, $entry);
225 close($REMOVE);
228 my @rawlines = ();
229 my @lines = ();
230 my $vname;
231 for my $filename (@ARGV) {
232 my $FILE;
233 if ($file) {
234 open($FILE, '-|', "diff -u /dev/null $filename") ||
235 die "$P: $filename: diff failed - $!\n";
236 } elsif ($filename eq '-') {
237 open($FILE, '<&STDIN');
238 } else {
239 open($FILE, '<', "$filename") ||
240 die "$P: $filename: open failed - $!\n";
242 if ($filename eq '-') {
243 $vname = 'Your patch';
244 } else {
245 $vname = $filename;
247 while (<$FILE>) {
248 chomp;
249 push(@rawlines, $_);
251 close($FILE);
252 if (!process($filename)) {
253 $exit = 1;
255 @rawlines = ();
256 @lines = ();
259 exit($exit);
261 sub top_of_kernel_tree {
262 my ($root) = @_;
264 my @tree_check = (
265 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
266 "README", "Documentation", "arch", "include", "drivers",
267 "fs", "init", "ipc", "kernel", "lib", "scripts",
270 foreach my $check (@tree_check) {
271 if (! -e $root . '/' . $check) {
272 return 0;
275 return 1;
278 sub expand_tabs {
279 my ($str) = @_;
281 my $res = '';
282 my $n = 0;
283 for my $c (split(//, $str)) {
284 if ($c eq "\t") {
285 $res .= ' ';
286 $n++;
287 for (; ($n % 8) != 0; $n++) {
288 $res .= ' ';
290 next;
292 $res .= $c;
293 $n++;
296 return $res;
298 sub copy_spacing {
299 (my $res = shift) =~ tr/\t/ /c;
300 return $res;
303 sub line_stats {
304 my ($line) = @_;
306 # Drop the diff line leader and expand tabs
307 $line =~ s/^.//;
308 $line = expand_tabs($line);
310 # Pick the indent from the front of the line.
311 my ($white) = ($line =~ /^(\s*)/);
313 return (length($line), length($white));
316 my $sanitise_quote = '';
318 sub sanitise_line_reset {
319 my ($in_comment) = @_;
321 if ($in_comment) {
322 $sanitise_quote = '*/';
323 } else {
324 $sanitise_quote = '';
327 sub sanitise_line {
328 my ($line) = @_;
330 my $res = '';
331 my $l = '';
333 my $qlen = 0;
334 my $off = 0;
335 my $c;
337 # Always copy over the diff marker.
338 $res = substr($line, 0, 1);
340 for ($off = 1; $off < length($line); $off++) {
341 $c = substr($line, $off, 1);
343 # Comments we are wacking completly including the begin
344 # and end, all to $;.
345 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
346 $sanitise_quote = '*/';
348 substr($res, $off, 2, "$;$;");
349 $off++;
350 next;
352 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
353 $sanitise_quote = '';
354 substr($res, $off, 2, "$;$;");
355 $off++;
356 next;
359 # A \ in a string means ignore the next character.
360 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
361 $c eq "\\") {
362 substr($res, $off, 2, 'XX');
363 $off++;
364 next;
366 # Regular quotes.
367 if ($c eq "'" || $c eq '"') {
368 if ($sanitise_quote eq '') {
369 $sanitise_quote = $c;
371 substr($res, $off, 1, $c);
372 next;
373 } elsif ($sanitise_quote eq $c) {
374 $sanitise_quote = '';
378 #print "c<$c> SQ<$sanitise_quote>\n";
379 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
380 substr($res, $off, 1, $;);
381 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
382 substr($res, $off, 1, 'X');
383 } else {
384 substr($res, $off, 1, $c);
388 # The pathname on a #include may be surrounded by '<' and '>'.
389 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
390 my $clean = 'X' x length($1);
391 $res =~ s@\<.*\>@<$clean>@;
393 # The whole of a #error is a string.
394 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
395 my $clean = 'X' x length($1);
396 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
399 return $res;
402 sub ctx_statement_block {
403 my ($linenr, $remain, $off) = @_;
404 my $line = $linenr - 1;
405 my $blk = '';
406 my $soff = $off;
407 my $coff = $off - 1;
408 my $coff_set = 0;
410 my $loff = 0;
412 my $type = '';
413 my $level = 0;
414 my @stack = ([$type, $level]);
415 my $p;
416 my $c;
417 my $len = 0;
419 my $remainder;
420 while (1) {
421 #warn "CSB: blk<$blk> remain<$remain>\n";
422 # If we are about to drop off the end, pull in more
423 # context.
424 if ($off >= $len) {
425 for (; $remain > 0; $line++) {
426 last if (!defined $lines[$line]);
427 next if ($lines[$line] =~ /^-/);
428 $remain--;
429 $loff = $len;
430 $blk .= $lines[$line] . "\n";
431 $len = length($blk);
432 $line++;
433 last;
435 # Bail if there is no further context.
436 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
437 if ($off >= $len) {
438 last;
441 $p = $c;
442 $c = substr($blk, $off, 1);
443 $remainder = substr($blk, $off);
445 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
447 # Handle nested #if/#else.
448 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
449 push(@stack, [ $type, $level ]);
450 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
451 ($type, $level) = @{$stack[$#stack - 1]};
452 } elsif ($remainder =~ /^#\s*endif\b/) {
453 ($type, $level) = @{pop(@stack)};
456 # Statement ends at the ';' or a close '}' at the
457 # outermost level.
458 if ($level == 0 && $c eq ';') {
459 last;
462 # An else is really a conditional as long as its not else if
463 if ($level == 0 && $coff_set == 0 &&
464 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
465 $remainder =~ /^(else)(?:\s|{)/ &&
466 $remainder !~ /^else\s+if\b/) {
467 $coff = $off + length($1) - 1;
468 $coff_set = 1;
469 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
470 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
473 if (($type eq '' || $type eq '(') && $c eq '(') {
474 $level++;
475 $type = '(';
477 if ($type eq '(' && $c eq ')') {
478 $level--;
479 $type = ($level != 0)? '(' : '';
481 if ($level == 0 && $coff < $soff) {
482 $coff = $off;
483 $coff_set = 1;
484 #warn "CSB: mark coff<$coff>\n";
487 if (($type eq '' || $type eq '{') && $c eq '{') {
488 $level++;
489 $type = '{';
491 if ($type eq '{' && $c eq '}') {
492 $level--;
493 $type = ($level != 0)? '{' : '';
495 if ($level == 0) {
496 last;
499 $off++;
501 # We are truly at the end, so shuffle to the next line.
502 if ($off == $len) {
503 $loff = $len + 1;
504 $line++;
505 $remain--;
508 my $statement = substr($blk, $soff, $off - $soff + 1);
509 my $condition = substr($blk, $soff, $coff - $soff + 1);
511 #warn "STATEMENT<$statement>\n";
512 #warn "CONDITION<$condition>\n";
514 #print "coff<$coff> soff<$off> loff<$loff>\n";
516 return ($statement, $condition,
517 $line, $remain + 1, $off - $loff + 1, $level);
520 sub statement_lines {
521 my ($stmt) = @_;
523 # Strip the diff line prefixes and rip blank lines at start and end.
524 $stmt =~ s/(^|\n)./$1/g;
525 $stmt =~ s/^\s*//;
526 $stmt =~ s/\s*$//;
528 my @stmt_lines = ($stmt =~ /\n/g);
530 return $#stmt_lines + 2;
533 sub statement_rawlines {
534 my ($stmt) = @_;
536 my @stmt_lines = ($stmt =~ /\n/g);
538 return $#stmt_lines + 2;
541 sub statement_block_size {
542 my ($stmt) = @_;
544 $stmt =~ s/(^|\n)./$1/g;
545 $stmt =~ s/^\s*{//;
546 $stmt =~ s/}\s*$//;
547 $stmt =~ s/^\s*//;
548 $stmt =~ s/\s*$//;
550 my @stmt_lines = ($stmt =~ /\n/g);
551 my @stmt_statements = ($stmt =~ /;/g);
553 my $stmt_lines = $#stmt_lines + 2;
554 my $stmt_statements = $#stmt_statements + 1;
556 if ($stmt_lines > $stmt_statements) {
557 return $stmt_lines;
558 } else {
559 return $stmt_statements;
563 sub ctx_statement_full {
564 my ($linenr, $remain, $off) = @_;
565 my ($statement, $condition, $level);
567 my (@chunks);
569 # Grab the first conditional/block pair.
570 ($statement, $condition, $linenr, $remain, $off, $level) =
571 ctx_statement_block($linenr, $remain, $off);
572 #print "F: c<$condition> s<$statement> remain<$remain>\n";
573 push(@chunks, [ $condition, $statement ]);
574 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
575 return ($level, $linenr, @chunks);
578 # Pull in the following conditional/block pairs and see if they
579 # could continue the statement.
580 for (;;) {
581 ($statement, $condition, $linenr, $remain, $off, $level) =
582 ctx_statement_block($linenr, $remain, $off);
583 #print "C: c<$condition> s<$statement> remain<$remain>\n";
584 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
585 #print "C: push\n";
586 push(@chunks, [ $condition, $statement ]);
589 return ($level, $linenr, @chunks);
592 sub ctx_block_get {
593 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
594 my $line;
595 my $start = $linenr - 1;
596 my $blk = '';
597 my @o;
598 my @c;
599 my @res = ();
601 my $level = 0;
602 my @stack = ($level);
603 for ($line = $start; $remain > 0; $line++) {
604 next if ($rawlines[$line] =~ /^-/);
605 $remain--;
607 $blk .= $rawlines[$line];
609 # Handle nested #if/#else.
610 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
611 push(@stack, $level);
612 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
613 $level = $stack[$#stack - 1];
614 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
615 $level = pop(@stack);
618 foreach my $c (split(//, $rawlines[$line])) {
619 ##print "C<$c>L<$level><$open$close>O<$off>\n";
620 if ($off > 0) {
621 $off--;
622 next;
625 if ($c eq $close && $level > 0) {
626 $level--;
627 last if ($level == 0);
628 } elsif ($c eq $open) {
629 $level++;
633 if (!$outer || $level <= 1) {
634 push(@res, $rawlines[$line]);
637 last if ($level == 0);
640 return ($level, @res);
642 sub ctx_block_outer {
643 my ($linenr, $remain) = @_;
645 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
646 return @r;
648 sub ctx_block {
649 my ($linenr, $remain) = @_;
651 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
652 return @r;
654 sub ctx_statement {
655 my ($linenr, $remain, $off) = @_;
657 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
658 return @r;
660 sub ctx_block_level {
661 my ($linenr, $remain) = @_;
663 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
665 sub ctx_statement_level {
666 my ($linenr, $remain, $off) = @_;
668 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
671 sub ctx_locate_comment {
672 my ($first_line, $end_line) = @_;
674 # Catch a comment on the end of the line itself.
675 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
676 return $current_comment if (defined $current_comment);
678 # Look through the context and try and figure out if there is a
679 # comment.
680 my $in_comment = 0;
681 $current_comment = '';
682 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
683 my $line = $rawlines[$linenr - 1];
684 #warn " $line\n";
685 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
686 $in_comment = 1;
688 if ($line =~ m@/\*@) {
689 $in_comment = 1;
691 if (!$in_comment && $current_comment ne '') {
692 $current_comment = '';
694 $current_comment .= $line . "\n" if ($in_comment);
695 if ($line =~ m@\*/@) {
696 $in_comment = 0;
700 chomp($current_comment);
701 return($current_comment);
703 sub ctx_has_comment {
704 my ($first_line, $end_line) = @_;
705 my $cmt = ctx_locate_comment($first_line, $end_line);
707 ##print "LINE: $rawlines[$end_line - 1 ]\n";
708 ##print "CMMT: $cmt\n";
710 return ($cmt ne '');
713 sub raw_line {
714 my ($linenr, $cnt) = @_;
716 my $offset = $linenr - 1;
717 $cnt++;
719 my $line;
720 while ($cnt) {
721 $line = $rawlines[$offset++];
722 next if (defined($line) && $line =~ /^-/);
723 $cnt--;
726 return $line;
729 sub cat_vet {
730 my ($vet) = @_;
731 my ($res, $coded);
733 $res = '';
734 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
735 $res .= $1;
736 if ($2 ne '') {
737 $coded = sprintf("^%c", unpack('C', $2) + 64);
738 $res .= $coded;
741 $res =~ s/$/\$/;
743 return $res;
746 my $av_preprocessor = 0;
747 my $av_pending;
748 my @av_paren_type;
749 my $av_pend_colon;
751 sub annotate_reset {
752 $av_preprocessor = 0;
753 $av_pending = '_';
754 @av_paren_type = ('E');
755 $av_pend_colon = 'O';
758 sub annotate_values {
759 my ($stream, $type) = @_;
761 my $res;
762 my $var = '_' x length($stream);
763 my $cur = $stream;
765 print "$stream\n" if ($dbg_values > 1);
767 while (length($cur)) {
768 @av_paren_type = ('E') if ($#av_paren_type < 0);
769 print " <" . join('', @av_paren_type) .
770 "> <$type> <$av_pending>" if ($dbg_values > 1);
771 if ($cur =~ /^(\s+)/o) {
772 print "WS($1)\n" if ($dbg_values > 1);
773 if ($1 =~ /\n/ && $av_preprocessor) {
774 $type = pop(@av_paren_type);
775 $av_preprocessor = 0;
778 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
779 print "DECLARE($1)\n" if ($dbg_values > 1);
780 $type = 'T';
782 } elsif ($cur =~ /^($Modifier)\s*/) {
783 print "MODIFIER($1)\n" if ($dbg_values > 1);
784 $type = 'T';
786 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
787 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
788 $av_preprocessor = 1;
789 push(@av_paren_type, $type);
790 if ($2 ne '') {
791 $av_pending = 'N';
793 $type = 'E';
795 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
796 print "UNDEF($1)\n" if ($dbg_values > 1);
797 $av_preprocessor = 1;
798 push(@av_paren_type, $type);
800 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
801 print "PRE_START($1)\n" if ($dbg_values > 1);
802 $av_preprocessor = 1;
804 push(@av_paren_type, $type);
805 push(@av_paren_type, $type);
806 $type = 'E';
808 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
809 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
810 $av_preprocessor = 1;
812 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
814 $type = 'E';
816 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
817 print "PRE_END($1)\n" if ($dbg_values > 1);
819 $av_preprocessor = 1;
821 # Assume all arms of the conditional end as this
822 # one does, and continue as if the #endif was not here.
823 pop(@av_paren_type);
824 push(@av_paren_type, $type);
825 $type = 'E';
827 } elsif ($cur =~ /^(\\\n)/o) {
828 print "PRECONT($1)\n" if ($dbg_values > 1);
830 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
831 print "ATTR($1)\n" if ($dbg_values > 1);
832 $av_pending = $type;
833 $type = 'N';
835 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
836 print "SIZEOF($1)\n" if ($dbg_values > 1);
837 if (defined $2) {
838 $av_pending = 'V';
840 $type = 'N';
842 } elsif ($cur =~ /^(if|while|for)\b/o) {
843 print "COND($1)\n" if ($dbg_values > 1);
844 $av_pending = 'E';
845 $type = 'N';
847 } elsif ($cur =~/^(case)/o) {
848 print "CASE($1)\n" if ($dbg_values > 1);
849 $av_pend_colon = 'C';
850 $type = 'N';
852 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
853 print "KEYWORD($1)\n" if ($dbg_values > 1);
854 $type = 'N';
856 } elsif ($cur =~ /^(\()/o) {
857 print "PAREN('$1')\n" if ($dbg_values > 1);
858 push(@av_paren_type, $av_pending);
859 $av_pending = '_';
860 $type = 'N';
862 } elsif ($cur =~ /^(\))/o) {
863 my $new_type = pop(@av_paren_type);
864 if ($new_type ne '_') {
865 $type = $new_type;
866 print "PAREN('$1') -> $type\n"
867 if ($dbg_values > 1);
868 } else {
869 print "PAREN('$1')\n" if ($dbg_values > 1);
872 } elsif ($cur =~ /^($Ident)\s*\(/o) {
873 print "FUNC($1)\n" if ($dbg_values > 1);
874 $type = 'V';
875 $av_pending = 'V';
877 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
878 if (defined $2 && $type eq 'C' || $type eq 'T') {
879 $av_pend_colon = 'B';
880 } elsif ($type eq 'E') {
881 $av_pend_colon = 'L';
883 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
884 $type = 'V';
886 } elsif ($cur =~ /^($Ident|$Constant)/o) {
887 print "IDENT($1)\n" if ($dbg_values > 1);
888 $type = 'V';
890 } elsif ($cur =~ /^($Assignment)/o) {
891 print "ASSIGN($1)\n" if ($dbg_values > 1);
892 $type = 'N';
894 } elsif ($cur =~/^(;|{|})/) {
895 print "END($1)\n" if ($dbg_values > 1);
896 $type = 'E';
897 $av_pend_colon = 'O';
899 } elsif ($cur =~/^(,)/) {
900 print "COMMA($1)\n" if ($dbg_values > 1);
901 $type = 'C';
903 } elsif ($cur =~ /^(\?)/o) {
904 print "QUESTION($1)\n" if ($dbg_values > 1);
905 $type = 'N';
907 } elsif ($cur =~ /^(:)/o) {
908 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
910 substr($var, length($res), 1, $av_pend_colon);
911 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
912 $type = 'E';
913 } else {
914 $type = 'N';
916 $av_pend_colon = 'O';
918 } elsif ($cur =~ /^(\[)/o) {
919 print "CLOSE($1)\n" if ($dbg_values > 1);
920 $type = 'N';
922 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
923 my $variant;
925 print "OPV($1)\n" if ($dbg_values > 1);
926 if ($type eq 'V') {
927 $variant = 'B';
928 } else {
929 $variant = 'U';
932 substr($var, length($res), 1, $variant);
933 $type = 'N';
935 } elsif ($cur =~ /^($Operators)/o) {
936 print "OP($1)\n" if ($dbg_values > 1);
937 if ($1 ne '++' && $1 ne '--') {
938 $type = 'N';
941 } elsif ($cur =~ /(^.)/o) {
942 print "C($1)\n" if ($dbg_values > 1);
944 if (defined $1) {
945 $cur = substr($cur, length($1));
946 $res .= $type x length($1);
950 return ($res, $var);
953 sub possible {
954 my ($possible, $line) = @_;
956 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
957 if ($possible !~ /(?:
958 ^(?:
959 $Modifier|
960 $Storage|
961 $Type|
962 DEFINE_\S+|
963 goto|
964 return|
965 case|
966 else|
967 asm|__asm__|
970 ^(?:typedef|struct|enum)\b
971 )/x) {
972 # Check for modifiers.
973 $possible =~ s/\s*$Storage\s*//g;
974 $possible =~ s/\s*$Sparse\s*//g;
975 if ($possible =~ /^\s*$/) {
977 } elsif ($possible =~ /\s/) {
978 $possible =~ s/\s*$Type\s*//g;
979 for my $modifier (split(' ', $possible)) {
980 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
981 push(@modifierList, $modifier);
984 } else {
985 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
986 push(@typeList, $possible);
988 build_types();
989 } else {
990 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
994 my $prefix = '';
996 sub report {
997 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
998 return 0;
1000 my $line = $prefix . $_[0];
1002 $line = (split('\n', $line))[0] . "\n" if ($terse);
1004 push(our @report, $line);
1006 return 1;
1008 sub report_dump {
1009 our @report;
1011 sub ERROR {
1012 if (report("ERROR: $_[0]\n")) {
1013 our $clean = 0;
1014 our $cnt_error++;
1017 sub WARN {
1018 if (report("WARNING: $_[0]\n")) {
1019 our $clean = 0;
1020 our $cnt_warn++;
1023 sub CHK {
1024 if ($check && report("CHECK: $_[0]\n")) {
1025 our $clean = 0;
1026 our $cnt_chk++;
1030 sub check_absolute_file {
1031 my ($absolute, $herecurr) = @_;
1032 my $file = $absolute;
1034 ##print "absolute<$absolute>\n";
1036 # See if any suffix of this path is a path within the tree.
1037 while ($file =~ s@^[^/]*/@@) {
1038 if (-f "$root/$file") {
1039 ##print "file<$file>\n";
1040 last;
1043 if (! -f _) {
1044 return 0;
1047 # It is, so see if the prefix is acceptable.
1048 my $prefix = $absolute;
1049 substr($prefix, -length($file)) = '';
1051 ##print "prefix<$prefix>\n";
1052 if ($prefix ne ".../") {
1053 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1057 sub process {
1058 my $filename = shift;
1060 my $linenr=0;
1061 my $prevline="";
1062 my $prevrawline="";
1063 my $stashline="";
1064 my $stashrawline="";
1066 my $length;
1067 my $indent;
1068 my $previndent=0;
1069 my $stashindent=0;
1071 our $clean = 1;
1072 my $signoff = 0;
1073 my $is_patch = 0;
1075 our @report = ();
1076 our $cnt_lines = 0;
1077 our $cnt_error = 0;
1078 our $cnt_warn = 0;
1079 our $cnt_chk = 0;
1081 # Trace the real file/line as we go.
1082 my $realfile = '';
1083 my $realline = 0;
1084 my $realcnt = 0;
1085 my $here = '';
1086 my $in_comment = 0;
1087 my $comment_edge = 0;
1088 my $first_line = 0;
1089 my $p1_prefix = '';
1091 my $prev_values = 'E';
1093 # suppression flags
1094 my %suppress_ifbraces;
1095 my %suppress_whiletrailers;
1097 # Pre-scan the patch sanitizing the lines.
1098 # Pre-scan the patch looking for any __setup documentation.
1100 my @setup_docs = ();
1101 my $setup_docs = 0;
1103 sanitise_line_reset();
1104 my $line;
1105 foreach my $rawline (@rawlines) {
1106 $linenr++;
1107 $line = $rawline;
1109 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1110 $setup_docs = 0;
1111 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1112 $setup_docs = 1;
1114 #next;
1116 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1117 $realline=$1-1;
1118 if (defined $2) {
1119 $realcnt=$3+1;
1120 } else {
1121 $realcnt=1+1;
1123 $in_comment = 0;
1125 # Guestimate if this is a continuing comment. Run
1126 # the context looking for a comment "edge". If this
1127 # edge is a close comment then we must be in a comment
1128 # at context start.
1129 my $edge;
1130 my $cnt = $realcnt;
1131 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1132 next if (defined $rawlines[$ln - 1] &&
1133 $rawlines[$ln - 1] =~ /^-/);
1134 $cnt--;
1135 #print "RAW<$rawlines[$ln - 1]>\n";
1136 last if (!defined $rawlines[$ln - 1]);
1137 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1138 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1139 ($edge) = $1;
1140 last;
1143 if (defined $edge && $edge eq '*/') {
1144 $in_comment = 1;
1147 # Guestimate if this is a continuing comment. If this
1148 # is the start of a diff block and this line starts
1149 # ' *' then it is very likely a comment.
1150 if (!defined $edge &&
1151 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1153 $in_comment = 1;
1156 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1157 sanitise_line_reset($in_comment);
1159 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1160 # Standardise the strings and chars within the input to
1161 # simplify matching -- only bother with positive lines.
1162 $line = sanitise_line($rawline);
1164 push(@lines, $line);
1166 if ($realcnt > 1) {
1167 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1168 } else {
1169 $realcnt = 0;
1172 #print "==>$rawline\n";
1173 #print "-->$line\n";
1175 if ($setup_docs && $line =~ /^\+/) {
1176 push(@setup_docs, $line);
1180 $prefix = '';
1182 $realcnt = 0;
1183 $linenr = 0;
1184 foreach my $line (@lines) {
1185 $linenr++;
1187 my $rawline = $rawlines[$linenr - 1];
1188 my $hunk_line = ($realcnt != 0);
1190 #extract the line range in the file after the patch is applied
1191 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1192 $is_patch = 1;
1193 $first_line = $linenr + 1;
1194 $realline=$1-1;
1195 if (defined $2) {
1196 $realcnt=$3+1;
1197 } else {
1198 $realcnt=1+1;
1200 annotate_reset();
1201 $prev_values = 'E';
1203 %suppress_ifbraces = ();
1204 %suppress_whiletrailers = ();
1205 next;
1207 # track the line number as we move through the hunk, note that
1208 # new versions of GNU diff omit the leading space on completely
1209 # blank context lines so we need to count that too.
1210 } elsif ($line =~ /^( |\+|$)/) {
1211 $realline++;
1212 $realcnt-- if ($realcnt != 0);
1214 # Measure the line length and indent.
1215 ($length, $indent) = line_stats($rawline);
1217 # Track the previous line.
1218 ($prevline, $stashline) = ($stashline, $line);
1219 ($previndent, $stashindent) = ($stashindent, $indent);
1220 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1222 #warn "line<$line>\n";
1224 } elsif ($realcnt == 1) {
1225 $realcnt--;
1228 #make up the handle for any error we report on this line
1229 $prefix = "$filename:$realline: " if ($emacs && $file);
1230 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1232 $here = "#$linenr: " if (!$file);
1233 $here = "#$realline: " if ($file);
1235 # extract the filename as it passes
1236 if ($line=~/^\+\+\+\s+(\S+)/) {
1237 $realfile = $1;
1238 $realfile =~ s@^([^/]*)/@@;
1240 $p1_prefix = $1;
1241 if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
1242 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1245 if ($realfile =~ m@^include/asm/@) {
1246 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1248 next;
1251 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1253 my $hereline = "$here\n$rawline\n";
1254 my $herecurr = "$here\n$rawline\n";
1255 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1257 $cnt_lines++ if ($realcnt != 0);
1259 #check the patch for a signoff:
1260 if ($line =~ /^\s*signed-off-by:/i) {
1261 # This is a signoff, if ugly, so do not double report.
1262 $signoff++;
1263 if (!($line =~ /^\s*Signed-off-by:/)) {
1264 WARN("Signed-off-by: is the preferred form\n" .
1265 $herecurr);
1267 if ($line =~ /^\s*signed-off-by:\S/i) {
1268 WARN("space required after Signed-off-by:\n" .
1269 $herecurr);
1273 # Check for wrappage within a valid hunk of the file
1274 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1275 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1276 $herecurr) if (!$emitted_corrupt++);
1279 # Check for absolute kernel paths.
1280 if ($tree) {
1281 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1282 my $file = $1;
1284 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1285 check_absolute_file($1, $herecurr)) {
1287 } else {
1288 check_absolute_file($file, $herecurr);
1293 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1294 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1295 $rawline !~ m/^$UTF8*$/) {
1296 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1298 my $blank = copy_spacing($rawline);
1299 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1300 my $hereptr = "$hereline$ptr\n";
1302 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1305 # ignore non-hunk lines and lines being removed
1306 next if (!$hunk_line || $line =~ /^-/);
1308 #trailing whitespace
1309 if ($line =~ /^\+.*\015/) {
1310 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1311 ERROR("DOS line endings\n" . $herevet);
1313 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1314 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1315 ERROR("trailing whitespace\n" . $herevet);
1318 # check we are in a valid source file if not then ignore this hunk
1319 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1321 #80 column limit
1322 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1323 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1324 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1325 $length > 80)
1327 WARN("line over 80 characters\n" . $herecurr);
1330 # check for adding lines without a newline.
1331 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1332 WARN("adding a line without newline at end of file\n" . $herecurr);
1335 # check we are in a valid source file C or perl if not then ignore this hunk
1336 next if ($realfile !~ /\.(h|c|pl)$/);
1338 # at the beginning of a line any tabs must come first and anything
1339 # more than 8 must use tabs.
1340 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1341 $rawline =~ /^\+\s* \s*/) {
1342 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1343 ERROR("code indent should use tabs where possible\n" . $herevet);
1346 # check we are in a valid C source file if not then ignore this hunk
1347 next if ($realfile !~ /\.(h|c)$/);
1349 # check for RCS/CVS revision markers
1350 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1351 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1354 # Check for potential 'bare' types
1355 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
1356 if ($realcnt && $line =~ /.\s*\S/) {
1357 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1358 ctx_statement_block($linenr, $realcnt, 0);
1359 $stat =~ s/\n./\n /g;
1360 $cond =~ s/\n./\n /g;
1362 my $s = $stat;
1363 $s =~ s/{.*$//s;
1365 # Ignore goto labels.
1366 if ($s =~ /$Ident:\*$/s) {
1368 # Ignore functions being called
1369 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1371 # declarations always start with types
1372 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1373 my $type = $1;
1374 $type =~ s/\s+/ /g;
1375 possible($type, "A:" . $s);
1377 # definitions in global scope can only start with types
1378 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1379 possible($1, "B:" . $s);
1382 # any (foo ... *) is a pointer cast, and foo is a type
1383 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1384 possible($1, "C:" . $s);
1387 # Check for any sort of function declaration.
1388 # int foo(something bar, other baz);
1389 # void (*store_gdt)(x86_descr_ptr *);
1390 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1391 my ($name_len) = length($1);
1393 my $ctx = $s;
1394 substr($ctx, 0, $name_len + 1, '');
1395 $ctx =~ s/\)[^\)]*$//;
1397 for my $arg (split(/\s*,\s*/, $ctx)) {
1398 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1400 possible($1, "D:" . $s);
1408 # Checks which may be anchored in the context.
1411 # Check for switch () and associated case and default
1412 # statements should be at the same indent.
1413 if ($line=~/\bswitch\s*\(.*\)/) {
1414 my $err = '';
1415 my $sep = '';
1416 my @ctx = ctx_block_outer($linenr, $realcnt);
1417 shift(@ctx);
1418 for my $ctx (@ctx) {
1419 my ($clen, $cindent) = line_stats($ctx);
1420 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1421 $indent != $cindent) {
1422 $err .= "$sep$ctx\n";
1423 $sep = '';
1424 } else {
1425 $sep = "[...]\n";
1428 if ($err ne '') {
1429 ERROR("switch and case should be at the same indent\n$hereline$err");
1433 # if/while/etc brace do not go on next line, unless defining a do while loop,
1434 # or if that brace on the next line is for something else
1435 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1436 my $pre_ctx = "$1$2";
1438 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1439 my $ctx_cnt = $realcnt - $#ctx - 1;
1440 my $ctx = join("\n", @ctx);
1442 my $ctx_ln = $linenr;
1443 my $ctx_skip = $realcnt;
1445 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1446 defined $lines[$ctx_ln - 1] &&
1447 $lines[$ctx_ln - 1] =~ /^-/)) {
1448 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1449 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1450 $ctx_ln++;
1453 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1454 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1456 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1457 ERROR("that open brace { should be on the previous line\n" .
1458 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1460 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1461 $ctx =~ /\)\s*\;\s*$/ &&
1462 defined $lines[$ctx_ln - 1])
1464 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1465 if ($nindent > $indent) {
1466 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1467 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1472 # Check relative indent for conditionals and blocks.
1473 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1474 my ($s, $c) = ($stat, $cond);
1476 substr($s, 0, length($c), '');
1478 # Make sure we remove the line prefixes as we have
1479 # none on the first line, and are going to readd them
1480 # where necessary.
1481 $s =~ s/\n./\n/gs;
1483 # Find out how long the conditional actually is.
1484 my @newlines = ($c =~ /\n/gs);
1485 my $cond_lines = 1 + $#newlines;
1487 # We want to check the first line inside the block
1488 # starting at the end of the conditional, so remove:
1489 # 1) any blank line termination
1490 # 2) any opening brace { on end of the line
1491 # 3) any do (...) {
1492 my $continuation = 0;
1493 my $check = 0;
1494 $s =~ s/^.*\bdo\b//;
1495 $s =~ s/^\s*{//;
1496 if ($s =~ s/^\s*\\//) {
1497 $continuation = 1;
1499 if ($s =~ s/^\s*?\n//) {
1500 $check = 1;
1501 $cond_lines++;
1504 # Also ignore a loop construct at the end of a
1505 # preprocessor statement.
1506 if (($prevline =~ /^.\s*#\s*define\s/ ||
1507 $prevline =~ /\\\s*$/) && $continuation == 0) {
1508 $check = 0;
1511 my $cond_ptr = -1;
1512 $continuation = 0;
1513 while ($cond_ptr != $cond_lines) {
1514 $cond_ptr = $cond_lines;
1516 # If we see an #else/#elif then the code
1517 # is not linear.
1518 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1519 $check = 0;
1522 # Ignore:
1523 # 1) blank lines, they should be at 0,
1524 # 2) preprocessor lines, and
1525 # 3) labels.
1526 if ($continuation ||
1527 $s =~ /^\s*?\n/ ||
1528 $s =~ /^\s*#\s*?/ ||
1529 $s =~ /^\s*$Ident\s*:/) {
1530 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1531 $s =~ s/^.*?\n//;
1532 $cond_lines++;
1536 my (undef, $sindent) = line_stats("+" . $s);
1537 my $stat_real = raw_line($linenr, $cond_lines);
1539 # Check if either of these lines are modified, else
1540 # this is not this patch's fault.
1541 if (!defined($stat_real) ||
1542 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1543 $check = 0;
1545 if (defined($stat_real) && $cond_lines > 1) {
1546 $stat_real = "[...]\n$stat_real";
1549 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
1551 if ($check && (($sindent % 8) != 0 ||
1552 ($sindent <= $indent && $s ne ''))) {
1553 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1557 # Track the 'values' across context and added lines.
1558 my $opline = $line; $opline =~ s/^./ /;
1559 my ($curr_values, $curr_vars) =
1560 annotate_values($opline . "\n", $prev_values);
1561 $curr_values = $prev_values . $curr_values;
1562 if ($dbg_values) {
1563 my $outline = $opline; $outline =~ s/\t/ /g;
1564 print "$linenr > .$outline\n";
1565 print "$linenr > $curr_values\n";
1566 print "$linenr > $curr_vars\n";
1568 $prev_values = substr($curr_values, -1);
1570 #ignore lines not being added
1571 if ($line=~/^[^\+]/) {next;}
1573 # TEST: allow direct testing of the type matcher.
1574 if ($dbg_type) {
1575 if ($line =~ /^.\s*$Declare\s*$/) {
1576 ERROR("TEST: is type\n" . $herecurr);
1577 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1578 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1580 next;
1582 # TEST: allow direct testing of the attribute matcher.
1583 if ($dbg_attr) {
1584 if ($line =~ /^.\s*$Attribute\s*$/) {
1585 ERROR("TEST: is attr\n" . $herecurr);
1586 } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1587 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1589 next;
1592 # check for initialisation to aggregates open brace on the next line
1593 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1594 $line =~ /^.\s*{/) {
1595 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1599 # Checks which are anchored on the added line.
1602 # check for malformed paths in #include statements (uses RAW line)
1603 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1604 my $path = $1;
1605 if ($path =~ m{//}) {
1606 ERROR("malformed #include filename\n" .
1607 $herecurr);
1611 # no C99 // comments
1612 if ($line =~ m{//}) {
1613 ERROR("do not use C99 // comments\n" . $herecurr);
1615 # Remove C99 comments.
1616 $line =~ s@//.*@@;
1617 $opline =~ s@//.*@@;
1619 #EXPORT_SYMBOL should immediately follow its function closing }.
1620 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1621 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1622 my $name = $1;
1623 if ($prevline !~ /(?:
1624 ^.}|
1625 ^.DEFINE_$Ident\(\Q$name\E\)|
1626 ^.DECLARE_$Ident\(\Q$name\E\)|
1627 ^.LIST_HEAD\(\Q$name\E\)|
1628 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1629 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1630 )/x) {
1631 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1635 # check for external initialisers.
1636 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1637 ERROR("do not initialise externals to 0 or NULL\n" .
1638 $herecurr);
1640 # check for static initialisers.
1641 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1642 ERROR("do not initialise statics to 0 or NULL\n" .
1643 $herecurr);
1646 # check for new typedefs, only function parameters and sparse annotations
1647 # make sense.
1648 if ($line =~ /\btypedef\s/ &&
1649 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1650 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1651 $line !~ /\b$typeTypedefs\b/ &&
1652 $line !~ /\b__bitwise(?:__|)\b/) {
1653 WARN("do not add new typedefs\n" . $herecurr);
1656 # * goes on variable not on type
1657 # (char*[ const])
1658 if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
1659 my ($from, $to) = ($1, $1);
1661 # Should start with a space.
1662 $to =~ s/^(\S)/ $1/;
1663 # Should not end with a space.
1664 $to =~ s/\s+$//;
1665 # '*'s should not have spaces between.
1666 while ($to =~ s/(.)\s\*/$1\*/) {
1669 #print "from<$from> to<$to>\n";
1670 if ($from ne $to) {
1671 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1673 } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
1674 my ($from, $to, $ident) = ($1, $1, $2);
1676 # Should start with a space.
1677 $to =~ s/^(\S)/ $1/;
1678 # Should not end with a space.
1679 $to =~ s/\s+$//;
1680 # '*'s should not have spaces between.
1681 while ($to =~ s/(.)\s\*/$1\*/) {
1683 # Modifiers should have spaces.
1684 $to =~ s/(\b$Modifier$)/$1 /;
1686 #print "from<$from> to<$to>\n";
1687 if ($from ne $to) {
1688 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1692 # # no BUG() or BUG_ON()
1693 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1694 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1695 # print "$herecurr";
1696 # $clean = 0;
1699 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1700 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1703 # printk should use KERN_* levels. Note that follow on printk's on the
1704 # same line do not need a level, so we use the current block context
1705 # to try and find and validate the current printk. In summary the current
1706 # printk includes all preceeding printk's which have no newline on the end.
1707 # we assume the first bad printk is the one to report.
1708 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1709 my $ok = 0;
1710 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1711 #print "CHECK<$lines[$ln - 1]\n";
1712 # we have a preceeding printk if it ends
1713 # with "\n" ignore it, else it is to blame
1714 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1715 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1716 $ok = 1;
1718 last;
1721 if ($ok == 0) {
1722 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1726 # function brace can't be on same line, except for #defines of do while,
1727 # or if closed on same line
1728 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1729 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1730 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1733 # open braces for enum, union and struct go on the same line.
1734 if ($line =~ /^.\s*{/ &&
1735 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1736 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1739 # check for spacing round square brackets; allowed:
1740 # 1. with a type on the left -- int [] a;
1741 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1742 # 3. inside a curly brace -- = { [0...10] = 5 }
1743 while ($line =~ /(.*?\s)\[/g) {
1744 my ($where, $prefix) = ($-[1], $1);
1745 if ($prefix !~ /$Type\s+$/ &&
1746 ($where != 0 || $prefix !~ /^.\s+$/) &&
1747 $prefix !~ /{\s+$/) {
1748 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1752 # check for spaces between functions and their parentheses.
1753 while ($line =~ /($Ident)\s+\(/g) {
1754 my $name = $1;
1755 my $ctx_before = substr($line, 0, $-[1]);
1756 my $ctx = "$ctx_before$name";
1758 # Ignore those directives where spaces _are_ permitted.
1759 if ($name =~ /^(?:
1760 if|for|while|switch|return|case|
1761 volatile|__volatile__|
1762 __attribute__|format|__extension__|
1763 asm|__asm__)$/x)
1766 # cpp #define statements have non-optional spaces, ie
1767 # if there is a space between the name and the open
1768 # parenthesis it is simply not a parameter group.
1769 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1771 # cpp #elif statement condition may start with a (
1772 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1774 # If this whole things ends with a type its most
1775 # likely a typedef for a function.
1776 } elsif ($ctx =~ /$Type$/) {
1778 } else {
1779 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1782 # Check operator spacing.
1783 if (!($line=~/\#\s*include/)) {
1784 my $ops = qr{
1785 <<=|>>=|<=|>=|==|!=|
1786 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1787 =>|->|<<|>>|<|>|=|!|~|
1788 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1789 \?|:
1791 my @elements = split(/($ops|;)/, $opline);
1792 my $off = 0;
1794 my $blank = copy_spacing($opline);
1796 for (my $n = 0; $n < $#elements; $n += 2) {
1797 $off += length($elements[$n]);
1799 # Pick up the preceeding and succeeding characters.
1800 my $ca = substr($opline, 0, $off);
1801 my $cc = '';
1802 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1803 $cc = substr($opline, $off + length($elements[$n + 1]));
1805 my $cb = "$ca$;$cc";
1807 my $a = '';
1808 $a = 'V' if ($elements[$n] ne '');
1809 $a = 'W' if ($elements[$n] =~ /\s$/);
1810 $a = 'C' if ($elements[$n] =~ /$;$/);
1811 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1812 $a = 'O' if ($elements[$n] eq '');
1813 $a = 'E' if ($ca =~ /^\s*$/);
1815 my $op = $elements[$n + 1];
1817 my $c = '';
1818 if (defined $elements[$n + 2]) {
1819 $c = 'V' if ($elements[$n + 2] ne '');
1820 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1821 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1822 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1823 $c = 'O' if ($elements[$n + 2] eq '');
1824 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1825 } else {
1826 $c = 'E';
1829 my $ctx = "${a}x${c}";
1831 my $at = "(ctx:$ctx)";
1833 my $ptr = substr($blank, 0, $off) . "^";
1834 my $hereptr = "$hereline$ptr\n";
1836 # Pull out the value of this operator.
1837 my $op_type = substr($curr_values, $off + 1, 1);
1839 # Get the full operator variant.
1840 my $opv = $op . substr($curr_vars, $off, 1);
1842 # Ignore operators passed as parameters.
1843 if ($op_type ne 'V' &&
1844 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1846 # # Ignore comments
1847 # } elsif ($op =~ /^$;+$/) {
1849 # ; should have either the end of line or a space or \ after it
1850 } elsif ($op eq ';') {
1851 if ($ctx !~ /.x[WEBC]/ &&
1852 $cc !~ /^\\/ && $cc !~ /^;/) {
1853 ERROR("space required after that '$op' $at\n" . $hereptr);
1856 # // is a comment
1857 } elsif ($op eq '//') {
1859 # No spaces for:
1860 # ->
1861 # : when part of a bitfield
1862 } elsif ($op eq '->' || $opv eq ':B') {
1863 if ($ctx =~ /Wx.|.xW/) {
1864 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1867 # , must have a space on the right.
1868 } elsif ($op eq ',') {
1869 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1870 ERROR("space required after that '$op' $at\n" . $hereptr);
1873 # '*' as part of a type definition -- reported already.
1874 } elsif ($opv eq '*_') {
1875 #warn "'*' is part of type\n";
1877 # unary operators should have a space before and
1878 # none after. May be left adjacent to another
1879 # unary operator, or a cast
1880 } elsif ($op eq '!' || $op eq '~' ||
1881 $opv eq '*U' || $opv eq '-U' ||
1882 $opv eq '&U' || $opv eq '&&U') {
1883 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1884 ERROR("space required before that '$op' $at\n" . $hereptr);
1886 if ($op eq '*' && $cc =~/\s*const\b/) {
1887 # A unary '*' may be const
1889 } elsif ($ctx =~ /.xW/) {
1890 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1893 # unary ++ and unary -- are allowed no space on one side.
1894 } elsif ($op eq '++' or $op eq '--') {
1895 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1896 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1898 if ($ctx =~ /Wx[BE]/ ||
1899 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1900 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1902 if ($ctx =~ /ExW/) {
1903 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1907 # << and >> may either have or not have spaces both sides
1908 } elsif ($op eq '<<' or $op eq '>>' or
1909 $op eq '&' or $op eq '^' or $op eq '|' or
1910 $op eq '+' or $op eq '-' or
1911 $op eq '*' or $op eq '/' or
1912 $op eq '%')
1914 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1915 ERROR("need consistent spacing around '$op' $at\n" .
1916 $hereptr);
1919 # A colon needs no spaces before when it is
1920 # terminating a case value or a label.
1921 } elsif ($opv eq ':C' || $opv eq ':L') {
1922 if ($ctx =~ /Wx./) {
1923 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1926 # All the others need spaces both sides.
1927 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1928 my $ok = 0;
1930 # Ignore email addresses <foo@bar>
1931 if (($op eq '<' &&
1932 $cc =~ /^\S+\@\S+>/) ||
1933 ($op eq '>' &&
1934 $ca =~ /<\S+\@\S+$/))
1936 $ok = 1;
1939 # Ignore ?:
1940 if (($opv eq ':O' && $ca =~ /\?$/) ||
1941 ($op eq '?' && $cc =~ /^:/)) {
1942 $ok = 1;
1945 if ($ok == 0) {
1946 ERROR("spaces required around that '$op' $at\n" . $hereptr);
1949 $off += length($elements[$n + 1]);
1953 # check for multiple assignments
1954 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1955 CHK("multiple assignments should be avoided\n" . $herecurr);
1958 ## # check for multiple declarations, allowing for a function declaration
1959 ## # continuation.
1960 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1961 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1963 ## # Remove any bracketed sections to ensure we do not
1964 ## # falsly report the parameters of functions.
1965 ## my $ln = $line;
1966 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1967 ## }
1968 ## if ($ln =~ /,/) {
1969 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1970 ## }
1971 ## }
1973 #need space before brace following if, while, etc
1974 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1975 $line =~ /do{/) {
1976 ERROR("space required before the open brace '{'\n" . $herecurr);
1979 # closing brace should have a space following it when it has anything
1980 # on the line
1981 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1982 ERROR("space required after that close brace '}'\n" . $herecurr);
1985 # check spacing on square brackets
1986 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1987 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
1989 if ($line =~ /\s\]/) {
1990 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1993 # check spacing on parentheses
1994 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1995 $line !~ /for\s*\(\s+;/) {
1996 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1998 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1999 $line !~ /for\s*\(.*;\s+\)/ &&
2000 $line !~ /:\s+\)/) {
2001 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2004 #goto labels aren't indented, allow a single space however
2005 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2006 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2007 WARN("labels should not be indented\n" . $herecurr);
2010 # Return is not a function.
2011 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2012 my $spacing = $1;
2013 my $value = $2;
2015 # Flatten any parentheses
2016 $value =~ s/\)\(/\) \(/g;
2017 while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) {
2020 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2021 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2023 } elsif ($spacing !~ /\s+/) {
2024 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2028 # Need a space before open parenthesis after if, while etc
2029 if ($line=~/\b(if|while|for|switch)\(/) {
2030 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2033 # Check for illegal assignment in if conditional -- and check for trailing
2034 # statements after the conditional.
2035 if ($line =~ /do\s*(?!{)/) {
2036 my ($stat_next) = ctx_statement_block($line_nr_next,
2037 $remain_next, $off_next);
2038 $stat_next =~ s/\n./\n /g;
2039 ##print "stat<$stat> stat_next<$stat_next>\n";
2041 if ($stat_next =~ /^\s*while\b/) {
2042 # If the statement carries leading newlines,
2043 # then count those as offsets.
2044 my ($whitespace) =
2045 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2046 my $offset =
2047 statement_rawlines($whitespace) - 1;
2049 $suppress_whiletrailers{$line_nr_next +
2050 $offset} = 1;
2053 if (!defined $suppress_whiletrailers{$linenr} &&
2054 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2055 my ($s, $c) = ($stat, $cond);
2057 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2058 ERROR("do not use assignment in if condition\n" . $herecurr);
2061 # Find out what is on the end of the line after the
2062 # conditional.
2063 substr($s, 0, length($c), '');
2064 $s =~ s/\n.*//g;
2065 $s =~ s/$;//g; # Remove any comments
2066 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2067 $c !~ /}\s*while\s*/)
2069 # Find out how long the conditional actually is.
2070 my @newlines = ($c =~ /\n/gs);
2071 my $cond_lines = 1 + $#newlines;
2073 my $stat_real = raw_line($linenr, $cond_lines);
2074 if (defined($stat_real) && $cond_lines > 1) {
2075 $stat_real = "[...]\n$stat_real";
2078 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2082 # Check for bitwise tests written as boolean
2083 if ($line =~ /
2085 (?:\[|\(|\&\&|\|\|)
2086 \s*0[xX][0-9]+\s*
2087 (?:\&\&|\|\|)
2089 (?:\&\&|\|\|)
2090 \s*0[xX][0-9]+\s*
2091 (?:\&\&|\|\||\)|\])
2092 )/x)
2094 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2097 # if and else should not have general statements after it
2098 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2099 my $s = $1;
2100 $s =~ s/$;//g; # Remove any comments
2101 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2102 ERROR("trailing statements should be on next line\n" . $herecurr);
2105 # case and default should not have general statements after them
2106 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2107 $line !~ /\G(?:
2108 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2109 \s*return\s+
2110 )/xg)
2112 ERROR("trailing statements should be on next line\n" . $herecurr);
2115 # Check for }<nl>else {, these must be at the same
2116 # indent level to be relevant to each other.
2117 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2118 $previndent == $indent) {
2119 ERROR("else should follow close brace '}'\n" . $hereprev);
2122 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2123 $previndent == $indent) {
2124 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2126 # Find out what is on the end of the line after the
2127 # conditional.
2128 substr($s, 0, length($c), '');
2129 $s =~ s/\n.*//g;
2131 if ($s =~ /^\s*;/) {
2132 ERROR("while should follow close brace '}'\n" . $hereprev);
2136 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2137 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2138 # print "No studly caps, use _\n";
2139 # print "$herecurr";
2140 # $clean = 0;
2143 #no spaces allowed after \ in define
2144 if ($line=~/\#\s*define.*\\\s$/) {
2145 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2148 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2149 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2150 my $file = "$1.h";
2151 my $checkfile = "include/linux/$file";
2152 if (-f "$root/$checkfile" &&
2153 $realfile ne $checkfile &&
2154 $1 ne 'irq')
2156 if ($realfile =~ m{^arch/}) {
2157 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2158 } else {
2159 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2164 # multi-statement macros should be enclosed in a do while loop, grab the
2165 # first statement and ensure its the whole macro if its not enclosed
2166 # in a known good container
2167 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2168 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2169 my $ln = $linenr;
2170 my $cnt = $realcnt;
2171 my ($off, $dstat, $dcond, $rest);
2172 my $ctx = '';
2174 my $args = defined($1);
2176 # Find the end of the macro and limit our statement
2177 # search to that.
2178 while ($cnt > 0 && defined $lines[$ln - 1] &&
2179 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2181 $ctx .= $rawlines[$ln - 1] . "\n";
2182 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2183 $ln++;
2185 $ctx .= $rawlines[$ln - 1];
2187 ($dstat, $dcond, $ln, $cnt, $off) =
2188 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2189 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2190 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2192 # Extract the remainder of the define (if any) and
2193 # rip off surrounding spaces, and trailing \'s.
2194 $rest = '';
2195 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2196 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2197 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2198 $rest .= substr($lines[$ln - 1], $off) . "\n";
2199 $cnt--;
2201 $ln++;
2202 $off = 0;
2204 $rest =~ s/\\\n.//g;
2205 $rest =~ s/^\s*//s;
2206 $rest =~ s/\s*$//s;
2208 # Clean up the original statement.
2209 if ($args) {
2210 substr($dstat, 0, length($dcond), '');
2211 } else {
2212 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2214 $dstat =~ s/$;//g;
2215 $dstat =~ s/\\\n.//g;
2216 $dstat =~ s/^\s*//s;
2217 $dstat =~ s/\s*$//s;
2219 # Flatten any parentheses and braces
2220 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2221 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2222 $dstat =~ s/\[[^\{\}]*\]/1/)
2226 my $exceptions = qr{
2227 $Declare|
2228 module_param_named|
2229 MODULE_PARAM_DESC|
2230 DECLARE_PER_CPU|
2231 DEFINE_PER_CPU|
2232 __typeof__\(|
2233 \.$Ident\s*=\s*
2235 #print "REST<$rest> dstat<$dstat>\n";
2236 if ($rest ne '') {
2237 if ($rest !~ /while\s*\(/ &&
2238 $dstat !~ /$exceptions/)
2240 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2243 } elsif ($ctx !~ /;/) {
2244 if ($dstat ne '' &&
2245 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2246 $dstat !~ /$exceptions/ &&
2247 $dstat !~ /^\.$Ident\s*=/ &&
2248 $dstat =~ /$Operators/)
2250 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2255 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2256 # all assignments may have only one of the following with an assignment:
2258 # ALIGN(...)
2259 # VMLINUX_SYMBOL(...)
2260 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2261 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2264 # check for redundant bracing round if etc
2265 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2266 my ($level, $endln, @chunks) =
2267 ctx_statement_full($linenr, $realcnt, 1);
2268 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2269 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2270 if ($#chunks > 0 && $level == 0) {
2271 my $allowed = 0;
2272 my $seen = 0;
2273 my $herectx = $here . "\n";
2274 my $ln = $linenr - 1;
2275 for my $chunk (@chunks) {
2276 my ($cond, $block) = @{$chunk};
2278 # If the condition carries leading newlines, then count those as offsets.
2279 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2280 my $offset = statement_rawlines($whitespace) - 1;
2282 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2284 # We have looked at and allowed this specific line.
2285 $suppress_ifbraces{$ln + $offset} = 1;
2287 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2288 $ln += statement_rawlines($block) - 1;
2290 substr($block, 0, length($cond), '');
2292 $seen++ if ($block =~ /^\s*{/);
2294 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2295 if (statement_lines($cond) > 1) {
2296 #print "APW: ALLOWED: cond<$cond>\n";
2297 $allowed = 1;
2299 if ($block =~/\b(?:if|for|while)\b/) {
2300 #print "APW: ALLOWED: block<$block>\n";
2301 $allowed = 1;
2303 if (statement_block_size($block) > 1) {
2304 #print "APW: ALLOWED: lines block<$block>\n";
2305 $allowed = 1;
2308 if ($seen && !$allowed) {
2309 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2313 if (!defined $suppress_ifbraces{$linenr - 1} &&
2314 $line =~ /\b(if|while|for|else)\b/) {
2315 my $allowed = 0;
2317 # Check the pre-context.
2318 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2319 #print "APW: ALLOWED: pre<$1>\n";
2320 $allowed = 1;
2323 my ($level, $endln, @chunks) =
2324 ctx_statement_full($linenr, $realcnt, $-[0]);
2326 # Check the condition.
2327 my ($cond, $block) = @{$chunks[0]};
2328 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2329 if (defined $cond) {
2330 substr($block, 0, length($cond), '');
2332 if (statement_lines($cond) > 1) {
2333 #print "APW: ALLOWED: cond<$cond>\n";
2334 $allowed = 1;
2336 if ($block =~/\b(?:if|for|while)\b/) {
2337 #print "APW: ALLOWED: block<$block>\n";
2338 $allowed = 1;
2340 if (statement_block_size($block) > 1) {
2341 #print "APW: ALLOWED: lines block<$block>\n";
2342 $allowed = 1;
2344 # Check the post-context.
2345 if (defined $chunks[1]) {
2346 my ($cond, $block) = @{$chunks[1]};
2347 if (defined $cond) {
2348 substr($block, 0, length($cond), '');
2350 if ($block =~ /^\s*\{/) {
2351 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2352 $allowed = 1;
2355 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2356 my $herectx = $here . "\n";;
2357 my $cnt = statement_rawlines($block);
2359 for (my $n = 0; $n < $cnt; $n++) {
2360 $herectx .= raw_line($linenr, $n) . "\n";;
2363 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2367 # don't include deprecated include files (uses RAW line)
2368 for my $inc (@dep_includes) {
2369 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2370 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2374 # don't use deprecated functions
2375 for my $func (@dep_functions) {
2376 if ($line =~ /\b$func\b/) {
2377 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2381 # no volatiles please
2382 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2383 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2384 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2387 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2388 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2389 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2392 # warn about #if 0
2393 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2394 CHK("if this code is redundant consider removing it\n" .
2395 $herecurr);
2398 # check for needless kfree() checks
2399 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2400 my $expr = $1;
2401 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2402 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2405 # check for needless usb_free_urb() checks
2406 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2407 my $expr = $1;
2408 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2409 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2413 # warn about #ifdefs in C files
2414 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2415 # print "#ifdef in C files should be avoided\n";
2416 # print "$herecurr";
2417 # $clean = 0;
2420 # warn about spacing in #ifdefs
2421 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2422 ERROR("exactly one space required after that #$1\n" . $herecurr);
2425 # check for spinlock_t definitions without a comment.
2426 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2427 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2428 my $which = $1;
2429 if (!ctx_has_comment($first_line, $linenr)) {
2430 CHK("$1 definition without comment\n" . $herecurr);
2433 # check for memory barriers without a comment.
2434 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2435 if (!ctx_has_comment($first_line, $linenr)) {
2436 CHK("memory barrier without comment\n" . $herecurr);
2439 # check of hardware specific defines
2440 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2441 CHK("architecture specific defines should be avoided\n" . $herecurr);
2444 # check the location of the inline attribute, that it is between
2445 # storage class and type.
2446 if ($line =~ /\b$Type\s+$Inline\b/ ||
2447 $line =~ /\b$Inline\s+$Storage\b/) {
2448 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2451 # Check for __inline__ and __inline, prefer inline
2452 if ($line =~ /\b(__inline__|__inline)\b/) {
2453 WARN("plain inline is preferred over $1\n" . $herecurr);
2456 # check for new externs in .c files.
2457 if ($realfile =~ /\.c$/ && defined $stat &&
2458 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2460 my $function_name = $1;
2461 my $paren_space = $2;
2463 my $s = $stat;
2464 if (defined $cond) {
2465 substr($s, 0, length($cond), '');
2467 if ($s =~ /^\s*;/ &&
2468 $function_name ne 'uninitialized_var')
2470 WARN("externs should be avoided in .c files\n" . $herecurr);
2473 if ($paren_space =~ /\n/) {
2474 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2477 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2478 $stat =~ /^.\s*extern\s+/)
2480 WARN("externs should be avoided in .c files\n" . $herecurr);
2483 # checks for new __setup's
2484 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2485 my $name = $1;
2487 if (!grep(/$name/, @setup_docs)) {
2488 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2492 # check for pointless casting of kmalloc return
2493 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2494 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2497 # check for gcc specific __FUNCTION__
2498 if ($line =~ /__FUNCTION__/) {
2499 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2502 # check for semaphores used as mutexes
2503 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2504 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2506 # check for semaphores used as mutexes
2507 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2508 WARN("consider using a completion\n" . $herecurr);
2510 # recommend strict_strto* over simple_strto*
2511 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2512 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2514 # check for __initcall(), use device_initcall() explicitly please
2515 if ($line =~ /^.\s*__initcall\s*\(/) {
2516 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2518 # check for struct file_operations, ensure they are const.
2519 if ($line =~ /\bstruct\s+file_operations\b/ &&
2520 $line !~ /\bconst\b/) {
2521 WARN("struct file_operations should normally be const\n" . $herecurr);
2524 # use of NR_CPUS is usually wrong
2525 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2526 if ($line =~ /\bNR_CPUS\b/ &&
2527 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2528 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2529 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2530 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2531 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2533 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2536 # check for %L{u,d,i} in strings
2537 my $string;
2538 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2539 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2540 $string =~ s/%%/__/g;
2541 if ($string =~ /(?<!%)%L[udi]/) {
2542 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2543 last;
2547 # whine mightly about in_atomic
2548 if ($line =~ /\bin_atomic\s*\(/) {
2549 if ($realfile =~ m@^drivers/@) {
2550 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2551 } else {
2552 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2557 # If we have no input at all, then there is nothing to report on
2558 # so just keep quiet.
2559 if ($#rawlines == -1) {
2560 exit(0);
2563 # In mailback mode only produce a report in the negative, for
2564 # things that appear to be patches.
2565 if ($mailback && ($clean == 1 || !$is_patch)) {
2566 exit(0);
2569 # This is not a patch, and we are are in 'no-patch' mode so
2570 # just keep quiet.
2571 if (!$chk_patch && !$is_patch) {
2572 exit(0);
2575 if (!$is_patch) {
2576 ERROR("Does not appear to be a unified-diff format patch\n");
2578 if ($is_patch && $chk_signoff && $signoff == 0) {
2579 ERROR("Missing Signed-off-by: line(s)\n");
2582 print report_dump();
2583 if ($summary && !($clean == 1 && $quiet == 1)) {
2584 print "$filename " if ($summary_file);
2585 print "total: $cnt_error errors, $cnt_warn warnings, " .
2586 (($check)? "$cnt_chk checks, " : "") .
2587 "$cnt_lines lines checked\n";
2588 print "\n" if ($quiet == 0);
2591 if ($clean == 1 && $quiet == 0) {
2592 print "$vname has no obvious style problems and is ready for submission.\n"
2594 if ($clean == 0 && $quiet == 0) {
2595 print "$vname has style problems, please review. If any of these errors\n";
2596 print "are false positives report them to the maintainer, see\n";
2597 print "CHECKPATCH in MAINTAINERS.\n";
2600 return $clean;