checkpatch: ensure we do not collapse bracketed sections into constants
[linux-2.6/x86.git] / scripts / checkpatch.pl
blob8d010ac0efe151b5bc1cd23237c935cb80b2724b
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. (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,2009, 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.30';
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 my $help = 0;
33 sub help {
34 my ($exitcode) = @_;
36 print << "EOM";
37 Usage: $P [OPTION]... [FILE]...
38 Version: $V
40 Options:
41 -q, --quiet quiet
42 --no-tree run without a kernel tree
43 --no-signoff do not check for 'Signed-off-by' line
44 --patch treat FILE as patchfile (default)
45 --emacs emacs compile window format
46 --terse one line per report
47 -f, --file treat FILE as regular source file
48 --subjective, --strict enable more subjective tests
49 --root=PATH PATH to the kernel tree root
50 --no-summary suppress the per-file summary
51 --mailback only produce a report in case of warnings/errors
52 --summary-file include the filename in summary
53 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
54 'values', 'possible', 'type', and 'attr' (default
55 is all off)
56 --test-only=WORD report only warnings/errors containing WORD
57 literally
58 -h, --help, --version display this help and exit
60 When FILE is - read standard input.
61 EOM
63 exit($exitcode);
66 GetOptions(
67 'q|quiet+' => \$quiet,
68 'tree!' => \$tree,
69 'signoff!' => \$chk_signoff,
70 'patch!' => \$chk_patch,
71 'emacs!' => \$emacs,
72 'terse!' => \$terse,
73 'f|file!' => \$file,
74 'subjective!' => \$check,
75 'strict!' => \$check,
76 'root=s' => \$root,
77 'summary!' => \$summary,
78 'mailback!' => \$mailback,
79 'summary-file!' => \$summary_file,
81 'debug=s' => \%debug,
82 'test-only=s' => \$tst_only,
83 'h|help' => \$help,
84 'version' => \$help
85 ) or help(1);
87 help(0) if ($help);
89 my $exit = 0;
91 if ($#ARGV < 0) {
92 print "$P: no input files\n";
93 exit(1);
96 my $dbg_values = 0;
97 my $dbg_possible = 0;
98 my $dbg_type = 0;
99 my $dbg_attr = 0;
100 for my $key (keys %debug) {
101 ## no critic
102 eval "\${dbg_$key} = '$debug{$key}';";
103 die "$@" if ($@);
106 my $rpt_cleaners = 0;
108 if ($terse) {
109 $emacs = 1;
110 $quiet++;
113 if ($tree) {
114 if (defined $root) {
115 if (!top_of_kernel_tree($root)) {
116 die "$P: $root: --root does not point at a valid tree\n";
118 } else {
119 if (top_of_kernel_tree('.')) {
120 $root = '.';
121 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
122 top_of_kernel_tree($1)) {
123 $root = $1;
127 if (!defined $root) {
128 print "Must be run from the top-level dir. of a kernel tree\n";
129 exit(2);
133 my $emitted_corrupt = 0;
135 our $Ident = qr{
136 [A-Za-z_][A-Za-z\d_]*
137 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
139 our $Storage = qr{extern|static|asmlinkage};
140 our $Sparse = qr{
141 __user|
142 __kernel|
143 __force|
144 __iomem|
145 __must_check|
146 __init_refok|
147 __kprobes|
148 __ref
151 # Notes to $Attribute:
152 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
153 our $Attribute = qr{
154 const|
155 __read_mostly|
156 __kprobes|
157 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
158 ____cacheline_aligned|
159 ____cacheline_aligned_in_smp|
160 ____cacheline_internodealigned_in_smp|
161 __weak
163 our $Modifier;
164 our $Inline = qr{inline|__always_inline|noinline};
165 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
166 our $Lval = qr{$Ident(?:$Member)*};
168 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
169 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
170 our $Compare = qr{<=|>=|==|!=|<|>};
171 our $Operators = qr{
172 <=|>=|==|!=|
173 =>|->|<<|>>|<|>|!|~|
174 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
177 our $NonptrType;
178 our $Type;
179 our $Declare;
181 our $UTF8 = qr {
182 [\x09\x0A\x0D\x20-\x7E] # ASCII
183 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
184 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
185 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
186 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
187 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
188 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
189 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
192 our $typeTypedefs = qr{(?x:
193 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
194 atomic_t
197 our $logFunctions = qr{(?x:
198 printk|
199 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
200 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
201 WARN|
202 panic
205 our @typeList = (
206 qr{void},
207 qr{(?:unsigned\s+)?char},
208 qr{(?:unsigned\s+)?short},
209 qr{(?:unsigned\s+)?int},
210 qr{(?:unsigned\s+)?long},
211 qr{(?:unsigned\s+)?long\s+int},
212 qr{(?:unsigned\s+)?long\s+long},
213 qr{(?:unsigned\s+)?long\s+long\s+int},
214 qr{unsigned},
215 qr{float},
216 qr{double},
217 qr{bool},
218 qr{struct\s+$Ident},
219 qr{union\s+$Ident},
220 qr{enum\s+$Ident},
221 qr{${Ident}_t},
222 qr{${Ident}_handler},
223 qr{${Ident}_handler_fn},
225 our @modifierList = (
226 qr{fastcall},
229 our $allowed_asm_includes = qr{(?x:
230 irq|
231 memory
233 # memory.h: ARM has a custom one
235 sub build_types {
236 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
237 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
238 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
239 $NonptrType = qr{
240 (?:$Modifier\s+|const\s+)*
242 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
243 (?:$typeTypedefs\b)|
244 (?:${all}\b)
246 (?:\s+$Modifier|\s+const)*
248 $Type = qr{
249 $NonptrType
250 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
251 (?:\s+$Inline|\s+$Modifier)*
253 $Declare = qr{(?:$Storage\s+)?$Type};
255 build_types();
257 $chk_signoff = 0 if ($file);
259 my @dep_includes = ();
260 my @dep_functions = ();
261 my $removal = "Documentation/feature-removal-schedule.txt";
262 if ($tree && -f "$root/$removal") {
263 open(my $REMOVE, '<', "$root/$removal") ||
264 die "$P: $removal: open failed - $!\n";
265 while (<$REMOVE>) {
266 if (/^Check:\s+(.*\S)/) {
267 for my $entry (split(/[, ]+/, $1)) {
268 if ($entry =~ m@include/(.*)@) {
269 push(@dep_includes, $1);
271 } elsif ($entry !~ m@/@) {
272 push(@dep_functions, $entry);
277 close($REMOVE);
280 my @rawlines = ();
281 my @lines = ();
282 my $vname;
283 for my $filename (@ARGV) {
284 my $FILE;
285 if ($file) {
286 open($FILE, '-|', "diff -u /dev/null $filename") ||
287 die "$P: $filename: diff failed - $!\n";
288 } elsif ($filename eq '-') {
289 open($FILE, '<&STDIN');
290 } else {
291 open($FILE, '<', "$filename") ||
292 die "$P: $filename: open failed - $!\n";
294 if ($filename eq '-') {
295 $vname = 'Your patch';
296 } else {
297 $vname = $filename;
299 while (<$FILE>) {
300 chomp;
301 push(@rawlines, $_);
303 close($FILE);
304 if (!process($filename)) {
305 $exit = 1;
307 @rawlines = ();
308 @lines = ();
311 exit($exit);
313 sub top_of_kernel_tree {
314 my ($root) = @_;
316 my @tree_check = (
317 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
318 "README", "Documentation", "arch", "include", "drivers",
319 "fs", "init", "ipc", "kernel", "lib", "scripts",
322 foreach my $check (@tree_check) {
323 if (! -e $root . '/' . $check) {
324 return 0;
327 return 1;
330 sub expand_tabs {
331 my ($str) = @_;
333 my $res = '';
334 my $n = 0;
335 for my $c (split(//, $str)) {
336 if ($c eq "\t") {
337 $res .= ' ';
338 $n++;
339 for (; ($n % 8) != 0; $n++) {
340 $res .= ' ';
342 next;
344 $res .= $c;
345 $n++;
348 return $res;
350 sub copy_spacing {
351 (my $res = shift) =~ tr/\t/ /c;
352 return $res;
355 sub line_stats {
356 my ($line) = @_;
358 # Drop the diff line leader and expand tabs
359 $line =~ s/^.//;
360 $line = expand_tabs($line);
362 # Pick the indent from the front of the line.
363 my ($white) = ($line =~ /^(\s*)/);
365 return (length($line), length($white));
368 my $sanitise_quote = '';
370 sub sanitise_line_reset {
371 my ($in_comment) = @_;
373 if ($in_comment) {
374 $sanitise_quote = '*/';
375 } else {
376 $sanitise_quote = '';
379 sub sanitise_line {
380 my ($line) = @_;
382 my $res = '';
383 my $l = '';
385 my $qlen = 0;
386 my $off = 0;
387 my $c;
389 # Always copy over the diff marker.
390 $res = substr($line, 0, 1);
392 for ($off = 1; $off < length($line); $off++) {
393 $c = substr($line, $off, 1);
395 # Comments we are wacking completly including the begin
396 # and end, all to $;.
397 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
398 $sanitise_quote = '*/';
400 substr($res, $off, 2, "$;$;");
401 $off++;
402 next;
404 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
405 $sanitise_quote = '';
406 substr($res, $off, 2, "$;$;");
407 $off++;
408 next;
410 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
411 $sanitise_quote = '//';
413 substr($res, $off, 2, $sanitise_quote);
414 $off++;
415 next;
418 # A \ in a string means ignore the next character.
419 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
420 $c eq "\\") {
421 substr($res, $off, 2, 'XX');
422 $off++;
423 next;
425 # Regular quotes.
426 if ($c eq "'" || $c eq '"') {
427 if ($sanitise_quote eq '') {
428 $sanitise_quote = $c;
430 substr($res, $off, 1, $c);
431 next;
432 } elsif ($sanitise_quote eq $c) {
433 $sanitise_quote = '';
437 #print "c<$c> SQ<$sanitise_quote>\n";
438 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
439 substr($res, $off, 1, $;);
440 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
441 substr($res, $off, 1, $;);
442 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
443 substr($res, $off, 1, 'X');
444 } else {
445 substr($res, $off, 1, $c);
449 if ($sanitise_quote eq '//') {
450 $sanitise_quote = '';
453 # The pathname on a #include may be surrounded by '<' and '>'.
454 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
455 my $clean = 'X' x length($1);
456 $res =~ s@\<.*\>@<$clean>@;
458 # The whole of a #error is a string.
459 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
460 my $clean = 'X' x length($1);
461 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
464 return $res;
467 sub ctx_statement_block {
468 my ($linenr, $remain, $off) = @_;
469 my $line = $linenr - 1;
470 my $blk = '';
471 my $soff = $off;
472 my $coff = $off - 1;
473 my $coff_set = 0;
475 my $loff = 0;
477 my $type = '';
478 my $level = 0;
479 my @stack = ();
480 my $p;
481 my $c;
482 my $len = 0;
484 my $remainder;
485 while (1) {
486 @stack = (['', 0]) if ($#stack == -1);
488 #warn "CSB: blk<$blk> remain<$remain>\n";
489 # If we are about to drop off the end, pull in more
490 # context.
491 if ($off >= $len) {
492 for (; $remain > 0; $line++) {
493 last if (!defined $lines[$line]);
494 next if ($lines[$line] =~ /^-/);
495 $remain--;
496 $loff = $len;
497 $blk .= $lines[$line] . "\n";
498 $len = length($blk);
499 $line++;
500 last;
502 # Bail if there is no further context.
503 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
504 if ($off >= $len) {
505 last;
508 $p = $c;
509 $c = substr($blk, $off, 1);
510 $remainder = substr($blk, $off);
512 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
514 # Handle nested #if/#else.
515 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
516 push(@stack, [ $type, $level ]);
517 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
518 ($type, $level) = @{$stack[$#stack - 1]};
519 } elsif ($remainder =~ /^#\s*endif\b/) {
520 ($type, $level) = @{pop(@stack)};
523 # Statement ends at the ';' or a close '}' at the
524 # outermost level.
525 if ($level == 0 && $c eq ';') {
526 last;
529 # An else is really a conditional as long as its not else if
530 if ($level == 0 && $coff_set == 0 &&
531 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
532 $remainder =~ /^(else)(?:\s|{)/ &&
533 $remainder !~ /^else\s+if\b/) {
534 $coff = $off + length($1) - 1;
535 $coff_set = 1;
536 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
537 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
540 if (($type eq '' || $type eq '(') && $c eq '(') {
541 $level++;
542 $type = '(';
544 if ($type eq '(' && $c eq ')') {
545 $level--;
546 $type = ($level != 0)? '(' : '';
548 if ($level == 0 && $coff < $soff) {
549 $coff = $off;
550 $coff_set = 1;
551 #warn "CSB: mark coff<$coff>\n";
554 if (($type eq '' || $type eq '{') && $c eq '{') {
555 $level++;
556 $type = '{';
558 if ($type eq '{' && $c eq '}') {
559 $level--;
560 $type = ($level != 0)? '{' : '';
562 if ($level == 0) {
563 if (substr($blk, $off + 1, 1) eq ';') {
564 $off++;
566 last;
569 $off++;
571 # We are truly at the end, so shuffle to the next line.
572 if ($off == $len) {
573 $loff = $len + 1;
574 $line++;
575 $remain--;
578 my $statement = substr($blk, $soff, $off - $soff + 1);
579 my $condition = substr($blk, $soff, $coff - $soff + 1);
581 #warn "STATEMENT<$statement>\n";
582 #warn "CONDITION<$condition>\n";
584 #print "coff<$coff> soff<$off> loff<$loff>\n";
586 return ($statement, $condition,
587 $line, $remain + 1, $off - $loff + 1, $level);
590 sub statement_lines {
591 my ($stmt) = @_;
593 # Strip the diff line prefixes and rip blank lines at start and end.
594 $stmt =~ s/(^|\n)./$1/g;
595 $stmt =~ s/^\s*//;
596 $stmt =~ s/\s*$//;
598 my @stmt_lines = ($stmt =~ /\n/g);
600 return $#stmt_lines + 2;
603 sub statement_rawlines {
604 my ($stmt) = @_;
606 my @stmt_lines = ($stmt =~ /\n/g);
608 return $#stmt_lines + 2;
611 sub statement_block_size {
612 my ($stmt) = @_;
614 $stmt =~ s/(^|\n)./$1/g;
615 $stmt =~ s/^\s*{//;
616 $stmt =~ s/}\s*$//;
617 $stmt =~ s/^\s*//;
618 $stmt =~ s/\s*$//;
620 my @stmt_lines = ($stmt =~ /\n/g);
621 my @stmt_statements = ($stmt =~ /;/g);
623 my $stmt_lines = $#stmt_lines + 2;
624 my $stmt_statements = $#stmt_statements + 1;
626 if ($stmt_lines > $stmt_statements) {
627 return $stmt_lines;
628 } else {
629 return $stmt_statements;
633 sub ctx_statement_full {
634 my ($linenr, $remain, $off) = @_;
635 my ($statement, $condition, $level);
637 my (@chunks);
639 # Grab the first conditional/block pair.
640 ($statement, $condition, $linenr, $remain, $off, $level) =
641 ctx_statement_block($linenr, $remain, $off);
642 #print "F: c<$condition> s<$statement> remain<$remain>\n";
643 push(@chunks, [ $condition, $statement ]);
644 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
645 return ($level, $linenr, @chunks);
648 # Pull in the following conditional/block pairs and see if they
649 # could continue the statement.
650 for (;;) {
651 ($statement, $condition, $linenr, $remain, $off, $level) =
652 ctx_statement_block($linenr, $remain, $off);
653 #print "C: c<$condition> s<$statement> remain<$remain>\n";
654 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
655 #print "C: push\n";
656 push(@chunks, [ $condition, $statement ]);
659 return ($level, $linenr, @chunks);
662 sub ctx_block_get {
663 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
664 my $line;
665 my $start = $linenr - 1;
666 my $blk = '';
667 my @o;
668 my @c;
669 my @res = ();
671 my $level = 0;
672 my @stack = ($level);
673 for ($line = $start; $remain > 0; $line++) {
674 next if ($rawlines[$line] =~ /^-/);
675 $remain--;
677 $blk .= $rawlines[$line];
679 # Handle nested #if/#else.
680 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
681 push(@stack, $level);
682 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
683 $level = $stack[$#stack - 1];
684 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
685 $level = pop(@stack);
688 foreach my $c (split(//, $rawlines[$line])) {
689 ##print "C<$c>L<$level><$open$close>O<$off>\n";
690 if ($off > 0) {
691 $off--;
692 next;
695 if ($c eq $close && $level > 0) {
696 $level--;
697 last if ($level == 0);
698 } elsif ($c eq $open) {
699 $level++;
703 if (!$outer || $level <= 1) {
704 push(@res, $rawlines[$line]);
707 last if ($level == 0);
710 return ($level, @res);
712 sub ctx_block_outer {
713 my ($linenr, $remain) = @_;
715 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
716 return @r;
718 sub ctx_block {
719 my ($linenr, $remain) = @_;
721 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
722 return @r;
724 sub ctx_statement {
725 my ($linenr, $remain, $off) = @_;
727 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
728 return @r;
730 sub ctx_block_level {
731 my ($linenr, $remain) = @_;
733 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
735 sub ctx_statement_level {
736 my ($linenr, $remain, $off) = @_;
738 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
741 sub ctx_locate_comment {
742 my ($first_line, $end_line) = @_;
744 # Catch a comment on the end of the line itself.
745 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
746 return $current_comment if (defined $current_comment);
748 # Look through the context and try and figure out if there is a
749 # comment.
750 my $in_comment = 0;
751 $current_comment = '';
752 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
753 my $line = $rawlines[$linenr - 1];
754 #warn " $line\n";
755 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
756 $in_comment = 1;
758 if ($line =~ m@/\*@) {
759 $in_comment = 1;
761 if (!$in_comment && $current_comment ne '') {
762 $current_comment = '';
764 $current_comment .= $line . "\n" if ($in_comment);
765 if ($line =~ m@\*/@) {
766 $in_comment = 0;
770 chomp($current_comment);
771 return($current_comment);
773 sub ctx_has_comment {
774 my ($first_line, $end_line) = @_;
775 my $cmt = ctx_locate_comment($first_line, $end_line);
777 ##print "LINE: $rawlines[$end_line - 1 ]\n";
778 ##print "CMMT: $cmt\n";
780 return ($cmt ne '');
783 sub raw_line {
784 my ($linenr, $cnt) = @_;
786 my $offset = $linenr - 1;
787 $cnt++;
789 my $line;
790 while ($cnt) {
791 $line = $rawlines[$offset++];
792 next if (defined($line) && $line =~ /^-/);
793 $cnt--;
796 return $line;
799 sub cat_vet {
800 my ($vet) = @_;
801 my ($res, $coded);
803 $res = '';
804 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
805 $res .= $1;
806 if ($2 ne '') {
807 $coded = sprintf("^%c", unpack('C', $2) + 64);
808 $res .= $coded;
811 $res =~ s/$/\$/;
813 return $res;
816 my $av_preprocessor = 0;
817 my $av_pending;
818 my @av_paren_type;
819 my $av_pend_colon;
821 sub annotate_reset {
822 $av_preprocessor = 0;
823 $av_pending = '_';
824 @av_paren_type = ('E');
825 $av_pend_colon = 'O';
828 sub annotate_values {
829 my ($stream, $type) = @_;
831 my $res;
832 my $var = '_' x length($stream);
833 my $cur = $stream;
835 print "$stream\n" if ($dbg_values > 1);
837 while (length($cur)) {
838 @av_paren_type = ('E') if ($#av_paren_type < 0);
839 print " <" . join('', @av_paren_type) .
840 "> <$type> <$av_pending>" if ($dbg_values > 1);
841 if ($cur =~ /^(\s+)/o) {
842 print "WS($1)\n" if ($dbg_values > 1);
843 if ($1 =~ /\n/ && $av_preprocessor) {
844 $type = pop(@av_paren_type);
845 $av_preprocessor = 0;
848 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
849 print "DECLARE($1)\n" if ($dbg_values > 1);
850 $type = 'T';
852 } elsif ($cur =~ /^($Modifier)\s*/) {
853 print "MODIFIER($1)\n" if ($dbg_values > 1);
854 $type = 'T';
856 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
857 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
858 $av_preprocessor = 1;
859 push(@av_paren_type, $type);
860 if ($2 ne '') {
861 $av_pending = 'N';
863 $type = 'E';
865 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
866 print "UNDEF($1)\n" if ($dbg_values > 1);
867 $av_preprocessor = 1;
868 push(@av_paren_type, $type);
870 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
871 print "PRE_START($1)\n" if ($dbg_values > 1);
872 $av_preprocessor = 1;
874 push(@av_paren_type, $type);
875 push(@av_paren_type, $type);
876 $type = 'E';
878 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
879 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
880 $av_preprocessor = 1;
882 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
884 $type = 'E';
886 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
887 print "PRE_END($1)\n" if ($dbg_values > 1);
889 $av_preprocessor = 1;
891 # Assume all arms of the conditional end as this
892 # one does, and continue as if the #endif was not here.
893 pop(@av_paren_type);
894 push(@av_paren_type, $type);
895 $type = 'E';
897 } elsif ($cur =~ /^(\\\n)/o) {
898 print "PRECONT($1)\n" if ($dbg_values > 1);
900 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
901 print "ATTR($1)\n" if ($dbg_values > 1);
902 $av_pending = $type;
903 $type = 'N';
905 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
906 print "SIZEOF($1)\n" if ($dbg_values > 1);
907 if (defined $2) {
908 $av_pending = 'V';
910 $type = 'N';
912 } elsif ($cur =~ /^(if|while|for)\b/o) {
913 print "COND($1)\n" if ($dbg_values > 1);
914 $av_pending = 'E';
915 $type = 'N';
917 } elsif ($cur =~/^(case)/o) {
918 print "CASE($1)\n" if ($dbg_values > 1);
919 $av_pend_colon = 'C';
920 $type = 'N';
922 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
923 print "KEYWORD($1)\n" if ($dbg_values > 1);
924 $type = 'N';
926 } elsif ($cur =~ /^(\()/o) {
927 print "PAREN('$1')\n" if ($dbg_values > 1);
928 push(@av_paren_type, $av_pending);
929 $av_pending = '_';
930 $type = 'N';
932 } elsif ($cur =~ /^(\))/o) {
933 my $new_type = pop(@av_paren_type);
934 if ($new_type ne '_') {
935 $type = $new_type;
936 print "PAREN('$1') -> $type\n"
937 if ($dbg_values > 1);
938 } else {
939 print "PAREN('$1')\n" if ($dbg_values > 1);
942 } elsif ($cur =~ /^($Ident)\s*\(/o) {
943 print "FUNC($1)\n" if ($dbg_values > 1);
944 $type = 'V';
945 $av_pending = 'V';
947 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
948 if (defined $2 && $type eq 'C' || $type eq 'T') {
949 $av_pend_colon = 'B';
950 } elsif ($type eq 'E') {
951 $av_pend_colon = 'L';
953 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
954 $type = 'V';
956 } elsif ($cur =~ /^($Ident|$Constant)/o) {
957 print "IDENT($1)\n" if ($dbg_values > 1);
958 $type = 'V';
960 } elsif ($cur =~ /^($Assignment)/o) {
961 print "ASSIGN($1)\n" if ($dbg_values > 1);
962 $type = 'N';
964 } elsif ($cur =~/^(;|{|})/) {
965 print "END($1)\n" if ($dbg_values > 1);
966 $type = 'E';
967 $av_pend_colon = 'O';
969 } elsif ($cur =~/^(,)/) {
970 print "COMMA($1)\n" if ($dbg_values > 1);
971 $type = 'C';
973 } elsif ($cur =~ /^(\?)/o) {
974 print "QUESTION($1)\n" if ($dbg_values > 1);
975 $type = 'N';
977 } elsif ($cur =~ /^(:)/o) {
978 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
980 substr($var, length($res), 1, $av_pend_colon);
981 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
982 $type = 'E';
983 } else {
984 $type = 'N';
986 $av_pend_colon = 'O';
988 } elsif ($cur =~ /^(\[)/o) {
989 print "CLOSE($1)\n" if ($dbg_values > 1);
990 $type = 'N';
992 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
993 my $variant;
995 print "OPV($1)\n" if ($dbg_values > 1);
996 if ($type eq 'V') {
997 $variant = 'B';
998 } else {
999 $variant = 'U';
1002 substr($var, length($res), 1, $variant);
1003 $type = 'N';
1005 } elsif ($cur =~ /^($Operators)/o) {
1006 print "OP($1)\n" if ($dbg_values > 1);
1007 if ($1 ne '++' && $1 ne '--') {
1008 $type = 'N';
1011 } elsif ($cur =~ /(^.)/o) {
1012 print "C($1)\n" if ($dbg_values > 1);
1014 if (defined $1) {
1015 $cur = substr($cur, length($1));
1016 $res .= $type x length($1);
1020 return ($res, $var);
1023 sub possible {
1024 my ($possible, $line) = @_;
1025 my $notPermitted = qr{(?:
1026 ^(?:
1027 $Modifier|
1028 $Storage|
1029 $Type|
1030 DEFINE_\S+
1032 ^(?:
1033 goto|
1034 return|
1035 case|
1036 else|
1037 asm|__asm__|
1039 )(?:\s|$)|
1040 ^(?:typedef|struct|enum)\b
1041 )}x;
1042 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1043 if ($possible !~ $notPermitted) {
1044 # Check for modifiers.
1045 $possible =~ s/\s*$Storage\s*//g;
1046 $possible =~ s/\s*$Sparse\s*//g;
1047 if ($possible =~ /^\s*$/) {
1049 } elsif ($possible =~ /\s/) {
1050 $possible =~ s/\s*$Type\s*//g;
1051 for my $modifier (split(' ', $possible)) {
1052 if ($modifier !~ $notPermitted) {
1053 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1054 push(@modifierList, $modifier);
1058 } else {
1059 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1060 push(@typeList, $possible);
1062 build_types();
1063 } else {
1064 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1068 my $prefix = '';
1070 sub report {
1071 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1072 return 0;
1074 my $line = $prefix . $_[0];
1076 $line = (split('\n', $line))[0] . "\n" if ($terse);
1078 push(our @report, $line);
1080 return 1;
1082 sub report_dump {
1083 our @report;
1085 sub ERROR {
1086 if (report("ERROR: $_[0]\n")) {
1087 our $clean = 0;
1088 our $cnt_error++;
1091 sub WARN {
1092 if (report("WARNING: $_[0]\n")) {
1093 our $clean = 0;
1094 our $cnt_warn++;
1097 sub CHK {
1098 if ($check && report("CHECK: $_[0]\n")) {
1099 our $clean = 0;
1100 our $cnt_chk++;
1104 sub check_absolute_file {
1105 my ($absolute, $herecurr) = @_;
1106 my $file = $absolute;
1108 ##print "absolute<$absolute>\n";
1110 # See if any suffix of this path is a path within the tree.
1111 while ($file =~ s@^[^/]*/@@) {
1112 if (-f "$root/$file") {
1113 ##print "file<$file>\n";
1114 last;
1117 if (! -f _) {
1118 return 0;
1121 # It is, so see if the prefix is acceptable.
1122 my $prefix = $absolute;
1123 substr($prefix, -length($file)) = '';
1125 ##print "prefix<$prefix>\n";
1126 if ($prefix ne ".../") {
1127 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1131 sub process {
1132 my $filename = shift;
1134 my $linenr=0;
1135 my $prevline="";
1136 my $prevrawline="";
1137 my $stashline="";
1138 my $stashrawline="";
1140 my $length;
1141 my $indent;
1142 my $previndent=0;
1143 my $stashindent=0;
1145 our $clean = 1;
1146 my $signoff = 0;
1147 my $is_patch = 0;
1149 our @report = ();
1150 our $cnt_lines = 0;
1151 our $cnt_error = 0;
1152 our $cnt_warn = 0;
1153 our $cnt_chk = 0;
1155 # Trace the real file/line as we go.
1156 my $realfile = '';
1157 my $realline = 0;
1158 my $realcnt = 0;
1159 my $here = '';
1160 my $in_comment = 0;
1161 my $comment_edge = 0;
1162 my $first_line = 0;
1163 my $p1_prefix = '';
1165 my $prev_values = 'E';
1167 # suppression flags
1168 my %suppress_ifbraces;
1169 my %suppress_whiletrailers;
1170 my %suppress_export;
1172 # Pre-scan the patch sanitizing the lines.
1173 # Pre-scan the patch looking for any __setup documentation.
1175 my @setup_docs = ();
1176 my $setup_docs = 0;
1178 sanitise_line_reset();
1179 my $line;
1180 foreach my $rawline (@rawlines) {
1181 $linenr++;
1182 $line = $rawline;
1184 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1185 $setup_docs = 0;
1186 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1187 $setup_docs = 1;
1189 #next;
1191 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1192 $realline=$1-1;
1193 if (defined $2) {
1194 $realcnt=$3+1;
1195 } else {
1196 $realcnt=1+1;
1198 $in_comment = 0;
1200 # Guestimate if this is a continuing comment. Run
1201 # the context looking for a comment "edge". If this
1202 # edge is a close comment then we must be in a comment
1203 # at context start.
1204 my $edge;
1205 my $cnt = $realcnt;
1206 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1207 next if (defined $rawlines[$ln - 1] &&
1208 $rawlines[$ln - 1] =~ /^-/);
1209 $cnt--;
1210 #print "RAW<$rawlines[$ln - 1]>\n";
1211 last if (!defined $rawlines[$ln - 1]);
1212 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1213 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1214 ($edge) = $1;
1215 last;
1218 if (defined $edge && $edge eq '*/') {
1219 $in_comment = 1;
1222 # Guestimate if this is a continuing comment. If this
1223 # is the start of a diff block and this line starts
1224 # ' *' then it is very likely a comment.
1225 if (!defined $edge &&
1226 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1228 $in_comment = 1;
1231 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1232 sanitise_line_reset($in_comment);
1234 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1235 # Standardise the strings and chars within the input to
1236 # simplify matching -- only bother with positive lines.
1237 $line = sanitise_line($rawline);
1239 push(@lines, $line);
1241 if ($realcnt > 1) {
1242 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1243 } else {
1244 $realcnt = 0;
1247 #print "==>$rawline\n";
1248 #print "-->$line\n";
1250 if ($setup_docs && $line =~ /^\+/) {
1251 push(@setup_docs, $line);
1255 $prefix = '';
1257 $realcnt = 0;
1258 $linenr = 0;
1259 foreach my $line (@lines) {
1260 $linenr++;
1262 my $rawline = $rawlines[$linenr - 1];
1264 #extract the line range in the file after the patch is applied
1265 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1266 $is_patch = 1;
1267 $first_line = $linenr + 1;
1268 $realline=$1-1;
1269 if (defined $2) {
1270 $realcnt=$3+1;
1271 } else {
1272 $realcnt=1+1;
1274 annotate_reset();
1275 $prev_values = 'E';
1277 %suppress_ifbraces = ();
1278 %suppress_whiletrailers = ();
1279 %suppress_export = ();
1280 next;
1282 # track the line number as we move through the hunk, note that
1283 # new versions of GNU diff omit the leading space on completely
1284 # blank context lines so we need to count that too.
1285 } elsif ($line =~ /^( |\+|$)/) {
1286 $realline++;
1287 $realcnt-- if ($realcnt != 0);
1289 # Measure the line length and indent.
1290 ($length, $indent) = line_stats($rawline);
1292 # Track the previous line.
1293 ($prevline, $stashline) = ($stashline, $line);
1294 ($previndent, $stashindent) = ($stashindent, $indent);
1295 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1297 #warn "line<$line>\n";
1299 } elsif ($realcnt == 1) {
1300 $realcnt--;
1303 my $hunk_line = ($realcnt != 0);
1305 #make up the handle for any error we report on this line
1306 $prefix = "$filename:$realline: " if ($emacs && $file);
1307 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1309 $here = "#$linenr: " if (!$file);
1310 $here = "#$realline: " if ($file);
1312 # extract the filename as it passes
1313 if ($line=~/^\+\+\+\s+(\S+)/) {
1314 $realfile = $1;
1315 $realfile =~ s@^([^/]*)/@@;
1317 $p1_prefix = $1;
1318 if (!$file && $tree && $p1_prefix ne '' &&
1319 -e "$root/$p1_prefix") {
1320 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1323 if ($realfile =~ m@^include/asm/@) {
1324 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1326 next;
1329 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1331 my $hereline = "$here\n$rawline\n";
1332 my $herecurr = "$here\n$rawline\n";
1333 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1335 $cnt_lines++ if ($realcnt != 0);
1337 #check the patch for a signoff:
1338 if ($line =~ /^\s*signed-off-by:/i) {
1339 # This is a signoff, if ugly, so do not double report.
1340 $signoff++;
1341 if (!($line =~ /^\s*Signed-off-by:/)) {
1342 WARN("Signed-off-by: is the preferred form\n" .
1343 $herecurr);
1345 if ($line =~ /^\s*signed-off-by:\S/i) {
1346 WARN("space required after Signed-off-by:\n" .
1347 $herecurr);
1351 # Check for wrappage within a valid hunk of the file
1352 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1353 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1354 $herecurr) if (!$emitted_corrupt++);
1357 # Check for absolute kernel paths.
1358 if ($tree) {
1359 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1360 my $file = $1;
1362 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1363 check_absolute_file($1, $herecurr)) {
1365 } else {
1366 check_absolute_file($file, $herecurr);
1371 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1372 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1373 $rawline !~ m/^$UTF8*$/) {
1374 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1376 my $blank = copy_spacing($rawline);
1377 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1378 my $hereptr = "$hereline$ptr\n";
1380 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1383 # ignore non-hunk lines and lines being removed
1384 next if (!$hunk_line || $line =~ /^-/);
1386 #trailing whitespace
1387 if ($line =~ /^\+.*\015/) {
1388 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1389 ERROR("DOS line endings\n" . $herevet);
1391 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1392 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1393 ERROR("trailing whitespace\n" . $herevet);
1394 $rpt_cleaners = 1;
1397 # check for Kconfig help text having a real description
1398 if ($realfile =~ /Kconfig/ &&
1399 $line =~ /\+?\s*(---)?help(---)?$/) {
1400 my $length = 0;
1401 for (my $l = $linenr; defined($lines[$l]); $l++) {
1402 my $f = $lines[$l];
1403 $f =~ s/#.*//;
1404 $f =~ s/^\s+//;
1405 next if ($f =~ /^$/);
1406 last if ($f =~ /^\s*config\s/);
1407 $length++;
1409 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
1412 # check we are in a valid source file if not then ignore this hunk
1413 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1415 #80 column limit
1416 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1417 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1418 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1419 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1420 $length > 80)
1422 WARN("line over 80 characters\n" . $herecurr);
1425 # check for spaces before a quoted newline
1426 if ($rawline =~ /^.*\".*\s\\n/) {
1427 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1430 # check for adding lines without a newline.
1431 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1432 WARN("adding a line without newline at end of file\n" . $herecurr);
1435 # Blackfin: use hi/lo macros
1436 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1437 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1438 my $herevet = "$here\n" . cat_vet($line) . "\n";
1439 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1441 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1442 my $herevet = "$here\n" . cat_vet($line) . "\n";
1443 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1447 # check we are in a valid source file C or perl if not then ignore this hunk
1448 next if ($realfile !~ /\.(h|c|pl)$/);
1450 # at the beginning of a line any tabs must come first and anything
1451 # more than 8 must use tabs.
1452 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1453 $rawline =~ /^\+\s* \s*/) {
1454 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1455 ERROR("code indent should use tabs where possible\n" . $herevet);
1456 $rpt_cleaners = 1;
1459 # check for space before tabs.
1460 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1461 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1462 WARN("please, no space before tabs\n" . $herevet);
1465 # check for spaces at the beginning of a line.
1466 # Exceptions:
1467 # 1) within comments
1468 # 2) indented preprocessor commands
1469 # 3) hanging labels
1470 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1471 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1472 WARN("please, no spaces at the start of a line\n" . $herevet);
1475 # check we are in a valid C source file if not then ignore this hunk
1476 next if ($realfile !~ /\.(h|c)$/);
1478 # check for RCS/CVS revision markers
1479 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1480 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1483 # Blackfin: don't use __builtin_bfin_[cs]sync
1484 if ($line =~ /__builtin_bfin_csync/) {
1485 my $herevet = "$here\n" . cat_vet($line) . "\n";
1486 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1488 if ($line =~ /__builtin_bfin_ssync/) {
1489 my $herevet = "$here\n" . cat_vet($line) . "\n";
1490 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1493 # Check for potential 'bare' types
1494 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1495 $realline_next);
1496 if ($realcnt && $line =~ /.\s*\S/) {
1497 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1498 ctx_statement_block($linenr, $realcnt, 0);
1499 $stat =~ s/\n./\n /g;
1500 $cond =~ s/\n./\n /g;
1502 # Find the real next line.
1503 $realline_next = $line_nr_next;
1504 if (defined $realline_next &&
1505 (!defined $lines[$realline_next - 1] ||
1506 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1507 $realline_next++;
1510 my $s = $stat;
1511 $s =~ s/{.*$//s;
1513 # Ignore goto labels.
1514 if ($s =~ /$Ident:\*$/s) {
1516 # Ignore functions being called
1517 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1519 } elsif ($s =~ /^.\s*else\b/s) {
1521 # declarations always start with types
1522 } 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) {
1523 my $type = $1;
1524 $type =~ s/\s+/ /g;
1525 possible($type, "A:" . $s);
1527 # definitions in global scope can only start with types
1528 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1529 possible($1, "B:" . $s);
1532 # any (foo ... *) is a pointer cast, and foo is a type
1533 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1534 possible($1, "C:" . $s);
1537 # Check for any sort of function declaration.
1538 # int foo(something bar, other baz);
1539 # void (*store_gdt)(x86_descr_ptr *);
1540 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1541 my ($name_len) = length($1);
1543 my $ctx = $s;
1544 substr($ctx, 0, $name_len + 1, '');
1545 $ctx =~ s/\)[^\)]*$//;
1547 for my $arg (split(/\s*,\s*/, $ctx)) {
1548 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1550 possible($1, "D:" . $s);
1558 # Checks which may be anchored in the context.
1561 # Check for switch () and associated case and default
1562 # statements should be at the same indent.
1563 if ($line=~/\bswitch\s*\(.*\)/) {
1564 my $err = '';
1565 my $sep = '';
1566 my @ctx = ctx_block_outer($linenr, $realcnt);
1567 shift(@ctx);
1568 for my $ctx (@ctx) {
1569 my ($clen, $cindent) = line_stats($ctx);
1570 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1571 $indent != $cindent) {
1572 $err .= "$sep$ctx\n";
1573 $sep = '';
1574 } else {
1575 $sep = "[...]\n";
1578 if ($err ne '') {
1579 ERROR("switch and case should be at the same indent\n$hereline$err");
1583 # if/while/etc brace do not go on next line, unless defining a do while loop,
1584 # or if that brace on the next line is for something else
1585 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1586 my $pre_ctx = "$1$2";
1588 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1589 my $ctx_cnt = $realcnt - $#ctx - 1;
1590 my $ctx = join("\n", @ctx);
1592 my $ctx_ln = $linenr;
1593 my $ctx_skip = $realcnt;
1595 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1596 defined $lines[$ctx_ln - 1] &&
1597 $lines[$ctx_ln - 1] =~ /^-/)) {
1598 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1599 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1600 $ctx_ln++;
1603 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1604 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1606 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1607 ERROR("that open brace { should be on the previous line\n" .
1608 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1610 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1611 $ctx =~ /\)\s*\;\s*$/ &&
1612 defined $lines[$ctx_ln - 1])
1614 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1615 if ($nindent > $indent) {
1616 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1617 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1622 # Check relative indent for conditionals and blocks.
1623 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1624 my ($s, $c) = ($stat, $cond);
1626 substr($s, 0, length($c), '');
1628 # Make sure we remove the line prefixes as we have
1629 # none on the first line, and are going to readd them
1630 # where necessary.
1631 $s =~ s/\n./\n/gs;
1633 # Find out how long the conditional actually is.
1634 my @newlines = ($c =~ /\n/gs);
1635 my $cond_lines = 1 + $#newlines;
1637 # We want to check the first line inside the block
1638 # starting at the end of the conditional, so remove:
1639 # 1) any blank line termination
1640 # 2) any opening brace { on end of the line
1641 # 3) any do (...) {
1642 my $continuation = 0;
1643 my $check = 0;
1644 $s =~ s/^.*\bdo\b//;
1645 $s =~ s/^\s*{//;
1646 if ($s =~ s/^\s*\\//) {
1647 $continuation = 1;
1649 if ($s =~ s/^\s*?\n//) {
1650 $check = 1;
1651 $cond_lines++;
1654 # Also ignore a loop construct at the end of a
1655 # preprocessor statement.
1656 if (($prevline =~ /^.\s*#\s*define\s/ ||
1657 $prevline =~ /\\\s*$/) && $continuation == 0) {
1658 $check = 0;
1661 my $cond_ptr = -1;
1662 $continuation = 0;
1663 while ($cond_ptr != $cond_lines) {
1664 $cond_ptr = $cond_lines;
1666 # If we see an #else/#elif then the code
1667 # is not linear.
1668 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1669 $check = 0;
1672 # Ignore:
1673 # 1) blank lines, they should be at 0,
1674 # 2) preprocessor lines, and
1675 # 3) labels.
1676 if ($continuation ||
1677 $s =~ /^\s*?\n/ ||
1678 $s =~ /^\s*#\s*?/ ||
1679 $s =~ /^\s*$Ident\s*:/) {
1680 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1681 if ($s =~ s/^.*?\n//) {
1682 $cond_lines++;
1687 my (undef, $sindent) = line_stats("+" . $s);
1688 my $stat_real = raw_line($linenr, $cond_lines);
1690 # Check if either of these lines are modified, else
1691 # this is not this patch's fault.
1692 if (!defined($stat_real) ||
1693 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1694 $check = 0;
1696 if (defined($stat_real) && $cond_lines > 1) {
1697 $stat_real = "[...]\n$stat_real";
1700 #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";
1702 if ($check && (($sindent % 8) != 0 ||
1703 ($sindent <= $indent && $s ne ''))) {
1704 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1708 # Track the 'values' across context and added lines.
1709 my $opline = $line; $opline =~ s/^./ /;
1710 my ($curr_values, $curr_vars) =
1711 annotate_values($opline . "\n", $prev_values);
1712 $curr_values = $prev_values . $curr_values;
1713 if ($dbg_values) {
1714 my $outline = $opline; $outline =~ s/\t/ /g;
1715 print "$linenr > .$outline\n";
1716 print "$linenr > $curr_values\n";
1717 print "$linenr > $curr_vars\n";
1719 $prev_values = substr($curr_values, -1);
1721 #ignore lines not being added
1722 if ($line=~/^[^\+]/) {next;}
1724 # TEST: allow direct testing of the type matcher.
1725 if ($dbg_type) {
1726 if ($line =~ /^.\s*$Declare\s*$/) {
1727 ERROR("TEST: is type\n" . $herecurr);
1728 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1729 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1731 next;
1733 # TEST: allow direct testing of the attribute matcher.
1734 if ($dbg_attr) {
1735 if ($line =~ /^.\s*$Modifier\s*$/) {
1736 ERROR("TEST: is attr\n" . $herecurr);
1737 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1738 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1740 next;
1743 # check for initialisation to aggregates open brace on the next line
1744 if ($line =~ /^.\s*{/ &&
1745 $prevline =~ /(?:^|[^=])=\s*$/) {
1746 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1750 # Checks which are anchored on the added line.
1753 # check for malformed paths in #include statements (uses RAW line)
1754 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1755 my $path = $1;
1756 if ($path =~ m{//}) {
1757 ERROR("malformed #include filename\n" .
1758 $herecurr);
1762 # no C99 // comments
1763 if ($line =~ m{//}) {
1764 ERROR("do not use C99 // comments\n" . $herecurr);
1766 # Remove C99 comments.
1767 $line =~ s@//.*@@;
1768 $opline =~ s@//.*@@;
1770 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1771 # the whole statement.
1772 #print "APW <$lines[$realline_next - 1]>\n";
1773 if (defined $realline_next &&
1774 exists $lines[$realline_next - 1] &&
1775 !defined $suppress_export{$realline_next} &&
1776 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1777 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1778 my $name = $1;
1779 if ($stat !~ /(?:
1780 \n.}\s*$|
1781 ^.DEFINE_$Ident\(\Q$name\E\)|
1782 ^.DECLARE_$Ident\(\Q$name\E\)|
1783 ^.LIST_HEAD\(\Q$name\E\)|
1784 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1785 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1786 )/x) {
1787 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1788 $suppress_export{$realline_next} = 2;
1789 } else {
1790 $suppress_export{$realline_next} = 1;
1793 if (!defined $suppress_export{$linenr} &&
1794 $prevline =~ /^.\s*$/ &&
1795 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1796 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1797 #print "FOO B <$lines[$linenr - 1]>\n";
1798 $suppress_export{$linenr} = 2;
1800 if (defined $suppress_export{$linenr} &&
1801 $suppress_export{$linenr} == 2) {
1802 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1805 # check for global initialisers.
1806 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1807 ERROR("do not initialise globals to 0 or NULL\n" .
1808 $herecurr);
1810 # check for static initialisers.
1811 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1812 ERROR("do not initialise statics to 0 or NULL\n" .
1813 $herecurr);
1816 # check for new typedefs, only function parameters and sparse annotations
1817 # make sense.
1818 if ($line =~ /\btypedef\s/ &&
1819 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1820 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1821 $line !~ /\b$typeTypedefs\b/ &&
1822 $line !~ /\b__bitwise(?:__|)\b/) {
1823 WARN("do not add new typedefs\n" . $herecurr);
1826 # * goes on variable not on type
1827 # (char*[ const])
1828 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1829 my ($from, $to) = ($1, $1);
1831 # Should start with a space.
1832 $to =~ s/^(\S)/ $1/;
1833 # Should not end with a space.
1834 $to =~ s/\s+$//;
1835 # '*'s should not have spaces between.
1836 while ($to =~ s/\*\s+\*/\*\*/) {
1839 #print "from<$from> to<$to>\n";
1840 if ($from ne $to) {
1841 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1843 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1844 my ($from, $to, $ident) = ($1, $1, $2);
1846 # Should start with a space.
1847 $to =~ s/^(\S)/ $1/;
1848 # Should not end with a space.
1849 $to =~ s/\s+$//;
1850 # '*'s should not have spaces between.
1851 while ($to =~ s/\*\s+\*/\*\*/) {
1853 # Modifiers should have spaces.
1854 $to =~ s/(\b$Modifier$)/$1 /;
1856 #print "from<$from> to<$to> ident<$ident>\n";
1857 if ($from ne $to && $ident !~ /^$Modifier$/) {
1858 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1862 # # no BUG() or BUG_ON()
1863 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1864 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1865 # print "$herecurr";
1866 # $clean = 0;
1869 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1870 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1873 # printk should use KERN_* levels. Note that follow on printk's on the
1874 # same line do not need a level, so we use the current block context
1875 # to try and find and validate the current printk. In summary the current
1876 # printk includes all preceeding printk's which have no newline on the end.
1877 # we assume the first bad printk is the one to report.
1878 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1879 my $ok = 0;
1880 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1881 #print "CHECK<$lines[$ln - 1]\n";
1882 # we have a preceeding printk if it ends
1883 # with "\n" ignore it, else it is to blame
1884 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1885 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1886 $ok = 1;
1888 last;
1891 if ($ok == 0) {
1892 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1896 # function brace can't be on same line, except for #defines of do while,
1897 # or if closed on same line
1898 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1899 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1900 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1903 # open braces for enum, union and struct go on the same line.
1904 if ($line =~ /^.\s*{/ &&
1905 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1906 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1909 # check for spacing round square brackets; allowed:
1910 # 1. with a type on the left -- int [] a;
1911 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1912 # 3. inside a curly brace -- = { [0...10] = 5 }
1913 while ($line =~ /(.*?\s)\[/g) {
1914 my ($where, $prefix) = ($-[1], $1);
1915 if ($prefix !~ /$Type\s+$/ &&
1916 ($where != 0 || $prefix !~ /^.\s+$/) &&
1917 $prefix !~ /{\s+$/) {
1918 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1922 # check for spaces between functions and their parentheses.
1923 while ($line =~ /($Ident)\s+\(/g) {
1924 my $name = $1;
1925 my $ctx_before = substr($line, 0, $-[1]);
1926 my $ctx = "$ctx_before$name";
1928 # Ignore those directives where spaces _are_ permitted.
1929 if ($name =~ /^(?:
1930 if|for|while|switch|return|case|
1931 volatile|__volatile__|
1932 __attribute__|format|__extension__|
1933 asm|__asm__)$/x)
1936 # cpp #define statements have non-optional spaces, ie
1937 # if there is a space between the name and the open
1938 # parenthesis it is simply not a parameter group.
1939 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1941 # cpp #elif statement condition may start with a (
1942 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1944 # If this whole things ends with a type its most
1945 # likely a typedef for a function.
1946 } elsif ($ctx =~ /$Type$/) {
1948 } else {
1949 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1952 # Check operator spacing.
1953 if (!($line=~/\#\s*include/)) {
1954 my $ops = qr{
1955 <<=|>>=|<=|>=|==|!=|
1956 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1957 =>|->|<<|>>|<|>|=|!|~|
1958 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1959 \?|:
1961 my @elements = split(/($ops|;)/, $opline);
1962 my $off = 0;
1964 my $blank = copy_spacing($opline);
1966 for (my $n = 0; $n < $#elements; $n += 2) {
1967 $off += length($elements[$n]);
1969 # Pick up the preceeding and succeeding characters.
1970 my $ca = substr($opline, 0, $off);
1971 my $cc = '';
1972 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1973 $cc = substr($opline, $off + length($elements[$n + 1]));
1975 my $cb = "$ca$;$cc";
1977 my $a = '';
1978 $a = 'V' if ($elements[$n] ne '');
1979 $a = 'W' if ($elements[$n] =~ /\s$/);
1980 $a = 'C' if ($elements[$n] =~ /$;$/);
1981 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1982 $a = 'O' if ($elements[$n] eq '');
1983 $a = 'E' if ($ca =~ /^\s*$/);
1985 my $op = $elements[$n + 1];
1987 my $c = '';
1988 if (defined $elements[$n + 2]) {
1989 $c = 'V' if ($elements[$n + 2] ne '');
1990 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1991 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1992 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1993 $c = 'O' if ($elements[$n + 2] eq '');
1994 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1995 } else {
1996 $c = 'E';
1999 my $ctx = "${a}x${c}";
2001 my $at = "(ctx:$ctx)";
2003 my $ptr = substr($blank, 0, $off) . "^";
2004 my $hereptr = "$hereline$ptr\n";
2006 # Pull out the value of this operator.
2007 my $op_type = substr($curr_values, $off + 1, 1);
2009 # Get the full operator variant.
2010 my $opv = $op . substr($curr_vars, $off, 1);
2012 # Ignore operators passed as parameters.
2013 if ($op_type ne 'V' &&
2014 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2016 # # Ignore comments
2017 # } elsif ($op =~ /^$;+$/) {
2019 # ; should have either the end of line or a space or \ after it
2020 } elsif ($op eq ';') {
2021 if ($ctx !~ /.x[WEBC]/ &&
2022 $cc !~ /^\\/ && $cc !~ /^;/) {
2023 ERROR("space required after that '$op' $at\n" . $hereptr);
2026 # // is a comment
2027 } elsif ($op eq '//') {
2029 # No spaces for:
2030 # ->
2031 # : when part of a bitfield
2032 } elsif ($op eq '->' || $opv eq ':B') {
2033 if ($ctx =~ /Wx.|.xW/) {
2034 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2037 # , must have a space on the right.
2038 } elsif ($op eq ',') {
2039 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2040 ERROR("space required after that '$op' $at\n" . $hereptr);
2043 # '*' as part of a type definition -- reported already.
2044 } elsif ($opv eq '*_') {
2045 #warn "'*' is part of type\n";
2047 # unary operators should have a space before and
2048 # none after. May be left adjacent to another
2049 # unary operator, or a cast
2050 } elsif ($op eq '!' || $op eq '~' ||
2051 $opv eq '*U' || $opv eq '-U' ||
2052 $opv eq '&U' || $opv eq '&&U') {
2053 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2054 ERROR("space required before that '$op' $at\n" . $hereptr);
2056 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2057 # A unary '*' may be const
2059 } elsif ($ctx =~ /.xW/) {
2060 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2063 # unary ++ and unary -- are allowed no space on one side.
2064 } elsif ($op eq '++' or $op eq '--') {
2065 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2066 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2068 if ($ctx =~ /Wx[BE]/ ||
2069 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2070 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2072 if ($ctx =~ /ExW/) {
2073 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2077 # << and >> may either have or not have spaces both sides
2078 } elsif ($op eq '<<' or $op eq '>>' or
2079 $op eq '&' or $op eq '^' or $op eq '|' or
2080 $op eq '+' or $op eq '-' or
2081 $op eq '*' or $op eq '/' or
2082 $op eq '%')
2084 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2085 ERROR("need consistent spacing around '$op' $at\n" .
2086 $hereptr);
2089 # A colon needs no spaces before when it is
2090 # terminating a case value or a label.
2091 } elsif ($opv eq ':C' || $opv eq ':L') {
2092 if ($ctx =~ /Wx./) {
2093 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2096 # All the others need spaces both sides.
2097 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2098 my $ok = 0;
2100 # Ignore email addresses <foo@bar>
2101 if (($op eq '<' &&
2102 $cc =~ /^\S+\@\S+>/) ||
2103 ($op eq '>' &&
2104 $ca =~ /<\S+\@\S+$/))
2106 $ok = 1;
2109 # Ignore ?:
2110 if (($opv eq ':O' && $ca =~ /\?$/) ||
2111 ($op eq '?' && $cc =~ /^:/)) {
2112 $ok = 1;
2115 if ($ok == 0) {
2116 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2119 $off += length($elements[$n + 1]);
2123 # check for multiple assignments
2124 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2125 CHK("multiple assignments should be avoided\n" . $herecurr);
2128 ## # check for multiple declarations, allowing for a function declaration
2129 ## # continuation.
2130 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2131 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2133 ## # Remove any bracketed sections to ensure we do not
2134 ## # falsly report the parameters of functions.
2135 ## my $ln = $line;
2136 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2137 ## }
2138 ## if ($ln =~ /,/) {
2139 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2140 ## }
2141 ## }
2143 #need space before brace following if, while, etc
2144 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2145 $line =~ /do{/) {
2146 ERROR("space required before the open brace '{'\n" . $herecurr);
2149 # closing brace should have a space following it when it has anything
2150 # on the line
2151 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2152 ERROR("space required after that close brace '}'\n" . $herecurr);
2155 # check spacing on square brackets
2156 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2157 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2159 if ($line =~ /\s\]/) {
2160 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2163 # check spacing on parentheses
2164 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2165 $line !~ /for\s*\(\s+;/) {
2166 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2168 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2169 $line !~ /for\s*\(.*;\s+\)/ &&
2170 $line !~ /:\s+\)/) {
2171 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2174 #goto labels aren't indented, allow a single space however
2175 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2176 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2177 WARN("labels should not be indented\n" . $herecurr);
2180 # Return is not a function.
2181 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2182 my $spacing = $1;
2183 my $value = $2;
2185 # Flatten any parentheses
2186 $value =~ s/\(/ \(/g;
2187 $value =~ s/\)/\) /g;
2188 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2189 $value !~ /(?:$Ident|-?$Constant)\s*
2190 $Compare\s*
2191 (?:$Ident|-?$Constant)/x &&
2192 $value =~ s/\([^\(\)]*\)/1/) {
2194 #print "value<$value>\n";
2195 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2196 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2198 } elsif ($spacing !~ /\s+/) {
2199 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2203 # Need a space before open parenthesis after if, while etc
2204 if ($line=~/\b(if|while|for|switch)\(/) {
2205 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2208 # Check for illegal assignment in if conditional -- and check for trailing
2209 # statements after the conditional.
2210 if ($line =~ /do\s*(?!{)/) {
2211 my ($stat_next) = ctx_statement_block($line_nr_next,
2212 $remain_next, $off_next);
2213 $stat_next =~ s/\n./\n /g;
2214 ##print "stat<$stat> stat_next<$stat_next>\n";
2216 if ($stat_next =~ /^\s*while\b/) {
2217 # If the statement carries leading newlines,
2218 # then count those as offsets.
2219 my ($whitespace) =
2220 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2221 my $offset =
2222 statement_rawlines($whitespace) - 1;
2224 $suppress_whiletrailers{$line_nr_next +
2225 $offset} = 1;
2228 if (!defined $suppress_whiletrailers{$linenr} &&
2229 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2230 my ($s, $c) = ($stat, $cond);
2232 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2233 ERROR("do not use assignment in if condition\n" . $herecurr);
2236 # Find out what is on the end of the line after the
2237 # conditional.
2238 substr($s, 0, length($c), '');
2239 $s =~ s/\n.*//g;
2240 $s =~ s/$;//g; # Remove any comments
2241 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2242 $c !~ /}\s*while\s*/)
2244 # Find out how long the conditional actually is.
2245 my @newlines = ($c =~ /\n/gs);
2246 my $cond_lines = 1 + $#newlines;
2247 my $stat_real = '';
2249 $stat_real = raw_line($linenr, $cond_lines)
2250 . "\n" if ($cond_lines);
2251 if (defined($stat_real) && $cond_lines > 1) {
2252 $stat_real = "[...]\n$stat_real";
2255 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2259 # Check for bitwise tests written as boolean
2260 if ($line =~ /
2262 (?:\[|\(|\&\&|\|\|)
2263 \s*0[xX][0-9]+\s*
2264 (?:\&\&|\|\|)
2266 (?:\&\&|\|\|)
2267 \s*0[xX][0-9]+\s*
2268 (?:\&\&|\|\||\)|\])
2269 )/x)
2271 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2274 # if and else should not have general statements after it
2275 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2276 my $s = $1;
2277 $s =~ s/$;//g; # Remove any comments
2278 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2279 ERROR("trailing statements should be on next line\n" . $herecurr);
2282 # if should not continue a brace
2283 if ($line =~ /}\s*if\b/) {
2284 ERROR("trailing statements should be on next line\n" .
2285 $herecurr);
2287 # case and default should not have general statements after them
2288 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2289 $line !~ /\G(?:
2290 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2291 \s*return\s+
2292 )/xg)
2294 ERROR("trailing statements should be on next line\n" . $herecurr);
2297 # Check for }<nl>else {, these must be at the same
2298 # indent level to be relevant to each other.
2299 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2300 $previndent == $indent) {
2301 ERROR("else should follow close brace '}'\n" . $hereprev);
2304 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2305 $previndent == $indent) {
2306 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2308 # Find out what is on the end of the line after the
2309 # conditional.
2310 substr($s, 0, length($c), '');
2311 $s =~ s/\n.*//g;
2313 if ($s =~ /^\s*;/) {
2314 ERROR("while should follow close brace '}'\n" . $hereprev);
2318 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2319 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2320 # print "No studly caps, use _\n";
2321 # print "$herecurr";
2322 # $clean = 0;
2325 #no spaces allowed after \ in define
2326 if ($line=~/\#\s*define.*\\\s$/) {
2327 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2330 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2331 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2332 my $file = "$1.h";
2333 my $checkfile = "include/linux/$file";
2334 if (-f "$root/$checkfile" &&
2335 $realfile ne $checkfile &&
2336 $1 !~ /$allowed_asm_includes/)
2338 if ($realfile =~ m{^arch/}) {
2339 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2340 } else {
2341 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2346 # multi-statement macros should be enclosed in a do while loop, grab the
2347 # first statement and ensure its the whole macro if its not enclosed
2348 # in a known good container
2349 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2350 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2351 my $ln = $linenr;
2352 my $cnt = $realcnt;
2353 my ($off, $dstat, $dcond, $rest);
2354 my $ctx = '';
2356 my $args = defined($1);
2358 # Find the end of the macro and limit our statement
2359 # search to that.
2360 while ($cnt > 0 && defined $lines[$ln - 1] &&
2361 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2363 $ctx .= $rawlines[$ln - 1] . "\n";
2364 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2365 $ln++;
2367 $ctx .= $rawlines[$ln - 1];
2369 ($dstat, $dcond, $ln, $cnt, $off) =
2370 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2371 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2372 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2374 # Extract the remainder of the define (if any) and
2375 # rip off surrounding spaces, and trailing \'s.
2376 $rest = '';
2377 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2378 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2379 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2380 $rest .= substr($lines[$ln - 1], $off) . "\n";
2381 $cnt--;
2383 $ln++;
2384 $off = 0;
2386 $rest =~ s/\\\n.//g;
2387 $rest =~ s/^\s*//s;
2388 $rest =~ s/\s*$//s;
2390 # Clean up the original statement.
2391 if ($args) {
2392 substr($dstat, 0, length($dcond), '');
2393 } else {
2394 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2396 $dstat =~ s/$;//g;
2397 $dstat =~ s/\\\n.//g;
2398 $dstat =~ s/^\s*//s;
2399 $dstat =~ s/\s*$//s;
2401 # Flatten any parentheses and braces
2402 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2403 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2404 $dstat =~ s/\[[^\{\}]*\]/1/)
2408 my $exceptions = qr{
2409 $Declare|
2410 module_param_named|
2411 MODULE_PARAM_DESC|
2412 DECLARE_PER_CPU|
2413 DEFINE_PER_CPU|
2414 __typeof__\(|
2415 union|
2416 struct|
2417 \.$Ident\s*=\s*|
2418 ^\"|\"$
2420 #print "REST<$rest> dstat<$dstat>\n";
2421 if ($rest ne '') {
2422 if ($rest !~ /while\s*\(/ &&
2423 $dstat !~ /$exceptions/)
2425 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2428 } elsif ($ctx !~ /;/) {
2429 if ($dstat ne '' &&
2430 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2431 $dstat !~ /$exceptions/ &&
2432 $dstat !~ /^\.$Ident\s*=/ &&
2433 $dstat =~ /$Operators/)
2435 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2440 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2441 # all assignments may have only one of the following with an assignment:
2443 # ALIGN(...)
2444 # VMLINUX_SYMBOL(...)
2445 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2446 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2449 # check for redundant bracing round if etc
2450 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2451 my ($level, $endln, @chunks) =
2452 ctx_statement_full($linenr, $realcnt, 1);
2453 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2454 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2455 if ($#chunks > 0 && $level == 0) {
2456 my $allowed = 0;
2457 my $seen = 0;
2458 my $herectx = $here . "\n";
2459 my $ln = $linenr - 1;
2460 for my $chunk (@chunks) {
2461 my ($cond, $block) = @{$chunk};
2463 # If the condition carries leading newlines, then count those as offsets.
2464 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2465 my $offset = statement_rawlines($whitespace) - 1;
2467 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2469 # We have looked at and allowed this specific line.
2470 $suppress_ifbraces{$ln + $offset} = 1;
2472 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2473 $ln += statement_rawlines($block) - 1;
2475 substr($block, 0, length($cond), '');
2477 $seen++ if ($block =~ /^\s*{/);
2479 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2480 if (statement_lines($cond) > 1) {
2481 #print "APW: ALLOWED: cond<$cond>\n";
2482 $allowed = 1;
2484 if ($block =~/\b(?:if|for|while)\b/) {
2485 #print "APW: ALLOWED: block<$block>\n";
2486 $allowed = 1;
2488 if (statement_block_size($block) > 1) {
2489 #print "APW: ALLOWED: lines block<$block>\n";
2490 $allowed = 1;
2493 if ($seen && !$allowed) {
2494 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2498 if (!defined $suppress_ifbraces{$linenr - 1} &&
2499 $line =~ /\b(if|while|for|else)\b/) {
2500 my $allowed = 0;
2502 # Check the pre-context.
2503 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2504 #print "APW: ALLOWED: pre<$1>\n";
2505 $allowed = 1;
2508 my ($level, $endln, @chunks) =
2509 ctx_statement_full($linenr, $realcnt, $-[0]);
2511 # Check the condition.
2512 my ($cond, $block) = @{$chunks[0]};
2513 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2514 if (defined $cond) {
2515 substr($block, 0, length($cond), '');
2517 if (statement_lines($cond) > 1) {
2518 #print "APW: ALLOWED: cond<$cond>\n";
2519 $allowed = 1;
2521 if ($block =~/\b(?:if|for|while)\b/) {
2522 #print "APW: ALLOWED: block<$block>\n";
2523 $allowed = 1;
2525 if (statement_block_size($block) > 1) {
2526 #print "APW: ALLOWED: lines block<$block>\n";
2527 $allowed = 1;
2529 # Check the post-context.
2530 if (defined $chunks[1]) {
2531 my ($cond, $block) = @{$chunks[1]};
2532 if (defined $cond) {
2533 substr($block, 0, length($cond), '');
2535 if ($block =~ /^\s*\{/) {
2536 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2537 $allowed = 1;
2540 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2541 my $herectx = $here . "\n";;
2542 my $cnt = statement_rawlines($block);
2544 for (my $n = 0; $n < $cnt; $n++) {
2545 $herectx .= raw_line($linenr, $n) . "\n";;
2548 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2552 # don't include deprecated include files (uses RAW line)
2553 for my $inc (@dep_includes) {
2554 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2555 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2559 # don't use deprecated functions
2560 for my $func (@dep_functions) {
2561 if ($line =~ /\b$func\b/) {
2562 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2566 # no volatiles please
2567 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2568 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2569 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2572 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2573 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2574 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2577 # warn about #if 0
2578 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2579 CHK("if this code is redundant consider removing it\n" .
2580 $herecurr);
2583 # check for needless kfree() checks
2584 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2585 my $expr = $1;
2586 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2587 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2590 # check for needless usb_free_urb() checks
2591 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2592 my $expr = $1;
2593 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2594 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2598 # prefer usleep_range over udelay
2599 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2600 # ignore udelay's < 10, however
2601 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2602 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2606 # warn about unexpectedly long msleep's
2607 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2608 if ($1 < 20) {
2609 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2613 # warn about #ifdefs in C files
2614 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2615 # print "#ifdef in C files should be avoided\n";
2616 # print "$herecurr";
2617 # $clean = 0;
2620 # warn about spacing in #ifdefs
2621 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2622 ERROR("exactly one space required after that #$1\n" . $herecurr);
2625 # check for spinlock_t definitions without a comment.
2626 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2627 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2628 my $which = $1;
2629 if (!ctx_has_comment($first_line, $linenr)) {
2630 CHK("$1 definition without comment\n" . $herecurr);
2633 # check for memory barriers without a comment.
2634 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2635 if (!ctx_has_comment($first_line, $linenr)) {
2636 CHK("memory barrier without comment\n" . $herecurr);
2639 # check of hardware specific defines
2640 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2641 CHK("architecture specific defines should be avoided\n" . $herecurr);
2644 # Check that the storage class is at the beginning of a declaration
2645 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2646 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2649 # check the location of the inline attribute, that it is between
2650 # storage class and type.
2651 if ($line =~ /\b$Type\s+$Inline\b/ ||
2652 $line =~ /\b$Inline\s+$Storage\b/) {
2653 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2656 # Check for __inline__ and __inline, prefer inline
2657 if ($line =~ /\b(__inline__|__inline)\b/) {
2658 WARN("plain inline is preferred over $1\n" . $herecurr);
2661 # check for sizeof(&)
2662 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2663 WARN("sizeof(& should be avoided\n" . $herecurr);
2666 # check for new externs in .c files.
2667 if ($realfile =~ /\.c$/ && defined $stat &&
2668 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2670 my $function_name = $1;
2671 my $paren_space = $2;
2673 my $s = $stat;
2674 if (defined $cond) {
2675 substr($s, 0, length($cond), '');
2677 if ($s =~ /^\s*;/ &&
2678 $function_name ne 'uninitialized_var')
2680 WARN("externs should be avoided in .c files\n" . $herecurr);
2683 if ($paren_space =~ /\n/) {
2684 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2687 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2688 $stat =~ /^.\s*extern\s+/)
2690 WARN("externs should be avoided in .c files\n" . $herecurr);
2693 # checks for new __setup's
2694 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2695 my $name = $1;
2697 if (!grep(/$name/, @setup_docs)) {
2698 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2702 # check for pointless casting of kmalloc return
2703 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2704 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2707 # check for gcc specific __FUNCTION__
2708 if ($line =~ /__FUNCTION__/) {
2709 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2712 # check for semaphores used as mutexes
2713 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2714 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2716 # check for semaphores used as mutexes
2717 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2718 WARN("consider using a completion\n" . $herecurr);
2721 # recommend strict_strto* over simple_strto*
2722 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2723 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2725 # check for __initcall(), use device_initcall() explicitly please
2726 if ($line =~ /^.\s*__initcall\s*\(/) {
2727 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2729 # check for various ops structs, ensure they are const.
2730 my $struct_ops = qr{acpi_dock_ops|
2731 address_space_operations|
2732 backlight_ops|
2733 block_device_operations|
2734 dentry_operations|
2735 dev_pm_ops|
2736 dma_map_ops|
2737 extent_io_ops|
2738 file_lock_operations|
2739 file_operations|
2740 hv_ops|
2741 ide_dma_ops|
2742 intel_dvo_dev_ops|
2743 item_operations|
2744 iwl_ops|
2745 kgdb_arch|
2746 kgdb_io|
2747 kset_uevent_ops|
2748 lock_manager_operations|
2749 microcode_ops|
2750 mtrr_ops|
2751 neigh_ops|
2752 nlmsvc_binding|
2753 pci_raw_ops|
2754 pipe_buf_operations|
2755 platform_hibernation_ops|
2756 platform_suspend_ops|
2757 proto_ops|
2758 rpc_pipe_ops|
2759 seq_operations|
2760 snd_ac97_build_ops|
2761 soc_pcmcia_socket_ops|
2762 stacktrace_ops|
2763 sysfs_ops|
2764 tty_operations|
2765 usb_mon_operations|
2766 wd_ops}x;
2767 if ($line !~ /\bconst\b/ &&
2768 $line =~ /\bstruct\s+($struct_ops)\b/) {
2769 WARN("struct $1 should normally be const\n" .
2770 $herecurr);
2773 # use of NR_CPUS is usually wrong
2774 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2775 if ($line =~ /\bNR_CPUS\b/ &&
2776 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2777 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2778 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2779 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2780 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2782 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2785 # check for %L{u,d,i} in strings
2786 my $string;
2787 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2788 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2789 $string =~ s/%%/__/g;
2790 if ($string =~ /(?<!%)%L[udi]/) {
2791 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2792 last;
2796 # whine mightly about in_atomic
2797 if ($line =~ /\bin_atomic\s*\(/) {
2798 if ($realfile =~ m@^drivers/@) {
2799 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2800 } elsif ($realfile !~ m@^kernel/@) {
2801 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2805 # check for lockdep_set_novalidate_class
2806 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2807 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2808 if ($realfile !~ m@^kernel/lockdep@ &&
2809 $realfile !~ m@^include/linux/lockdep@ &&
2810 $realfile !~ m@^drivers/base/core@) {
2811 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2816 # If we have no input at all, then there is nothing to report on
2817 # so just keep quiet.
2818 if ($#rawlines == -1) {
2819 exit(0);
2822 # In mailback mode only produce a report in the negative, for
2823 # things that appear to be patches.
2824 if ($mailback && ($clean == 1 || !$is_patch)) {
2825 exit(0);
2828 # This is not a patch, and we are are in 'no-patch' mode so
2829 # just keep quiet.
2830 if (!$chk_patch && !$is_patch) {
2831 exit(0);
2834 if (!$is_patch) {
2835 ERROR("Does not appear to be a unified-diff format patch\n");
2837 if ($is_patch && $chk_signoff && $signoff == 0) {
2838 ERROR("Missing Signed-off-by: line(s)\n");
2841 print report_dump();
2842 if ($summary && !($clean == 1 && $quiet == 1)) {
2843 print "$filename " if ($summary_file);
2844 print "total: $cnt_error errors, $cnt_warn warnings, " .
2845 (($check)? "$cnt_chk checks, " : "") .
2846 "$cnt_lines lines checked\n";
2847 print "\n" if ($quiet == 0);
2850 if ($quiet == 0) {
2851 # If there were whitespace errors which cleanpatch can fix
2852 # then suggest that.
2853 if ($rpt_cleaners) {
2854 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2855 print " scripts/cleanfile\n\n";
2859 if ($clean == 1 && $quiet == 0) {
2860 print "$vname has no obvious style problems and is ready for submission.\n"
2862 if ($clean == 0 && $quiet == 0) {
2863 print "$vname has style problems, please review. If any of these errors\n";
2864 print "are false positives report them to the maintainer, see\n";
2865 print "CHECKPATCH in MAINTAINERS.\n";
2868 return $clean;