PPC: E500: Use generic kvm function for freq
[qemu/agraf.git] / scripts / checkpatch.pl
blob0eba357cc2105be70a7738601be552801bc5b5bd
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-2010 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.31';
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 __percpu|
156 __nocast|
157 __safe|
158 __bitwise__|
159 __packed__|
160 __packed2__|
161 __naked|
162 __maybe_unused|
163 __always_unused|
164 __noreturn|
165 __used|
166 __cold|
167 __noclone|
168 __deprecated|
169 __read_mostly|
170 __kprobes|
171 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
172 ____cacheline_aligned|
173 ____cacheline_aligned_in_smp|
174 ____cacheline_internodealigned_in_smp|
175 __weak
177 our $Modifier;
178 our $Inline = qr{inline|__always_inline|noinline};
179 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
180 our $Lval = qr{$Ident(?:$Member)*};
182 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
183 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
184 our $Compare = qr{<=|>=|==|!=|<|>};
185 our $Operators = qr{
186 <=|>=|==|!=|
187 =>|->|<<|>>|<|>|!|~|
188 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
191 our $NonptrType;
192 our $Type;
193 our $Declare;
195 our $UTF8 = qr {
196 [\x09\x0A\x0D\x20-\x7E] # ASCII
197 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
198 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
199 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
200 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
201 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
202 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
203 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
206 our $typeTypedefs = qr{(?x:
207 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
208 atomic_t
211 our $logFunctions = qr{(?x:
212 printk|
213 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
214 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
215 WARN|
216 panic
219 our @typeList = (
220 qr{void},
221 qr{(?:unsigned\s+)?char},
222 qr{(?:unsigned\s+)?short},
223 qr{(?:unsigned\s+)?int},
224 qr{(?:unsigned\s+)?long},
225 qr{(?:unsigned\s+)?long\s+int},
226 qr{(?:unsigned\s+)?long\s+long},
227 qr{(?:unsigned\s+)?long\s+long\s+int},
228 qr{unsigned},
229 qr{float},
230 qr{double},
231 qr{bool},
232 qr{struct\s+$Ident},
233 qr{union\s+$Ident},
234 qr{enum\s+$Ident},
235 qr{${Ident}_t},
236 qr{${Ident}_handler},
237 qr{${Ident}_handler_fn},
239 our @modifierList = (
240 qr{fastcall},
243 our $allowed_asm_includes = qr{(?x:
244 irq|
245 memory
247 # memory.h: ARM has a custom one
249 sub build_types {
250 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
251 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
252 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
253 $NonptrType = qr{
254 (?:$Modifier\s+|const\s+)*
256 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
257 (?:$typeTypedefs\b)|
258 (?:${all}\b)
260 (?:\s+$Modifier|\s+const)*
262 $Type = qr{
263 $NonptrType
264 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
265 (?:\s+$Inline|\s+$Modifier)*
267 $Declare = qr{(?:$Storage\s+)?$Type};
269 build_types();
271 $chk_signoff = 0 if ($file);
273 my @dep_includes = ();
274 my @dep_functions = ();
275 my $removal = "Documentation/feature-removal-schedule.txt";
276 if ($tree && -f "$root/$removal") {
277 open(my $REMOVE, '<', "$root/$removal") ||
278 die "$P: $removal: open failed - $!\n";
279 while (<$REMOVE>) {
280 if (/^Check:\s+(.*\S)/) {
281 for my $entry (split(/[, ]+/, $1)) {
282 if ($entry =~ m@include/(.*)@) {
283 push(@dep_includes, $1);
285 } elsif ($entry !~ m@/@) {
286 push(@dep_functions, $entry);
291 close($REMOVE);
294 my @rawlines = ();
295 my @lines = ();
296 my $vname;
297 for my $filename (@ARGV) {
298 my $FILE;
299 if ($file) {
300 open($FILE, '-|', "diff -u /dev/null $filename") ||
301 die "$P: $filename: diff failed - $!\n";
302 } elsif ($filename eq '-') {
303 open($FILE, '<&STDIN');
304 } else {
305 open($FILE, '<', "$filename") ||
306 die "$P: $filename: open failed - $!\n";
308 if ($filename eq '-') {
309 $vname = 'Your patch';
310 } else {
311 $vname = $filename;
313 while (<$FILE>) {
314 chomp;
315 push(@rawlines, $_);
317 close($FILE);
318 if (!process($filename)) {
319 $exit = 1;
321 @rawlines = ();
322 @lines = ();
325 exit($exit);
327 sub top_of_kernel_tree {
328 my ($root) = @_;
330 my @tree_check = (
331 "COPYING", "MAINTAINERS", "Makefile",
332 "README", "docs", "VERSION",
333 "vl.c"
336 foreach my $check (@tree_check) {
337 if (! -e $root . '/' . $check) {
338 return 0;
341 return 1;
344 sub expand_tabs {
345 my ($str) = @_;
347 my $res = '';
348 my $n = 0;
349 for my $c (split(//, $str)) {
350 if ($c eq "\t") {
351 $res .= ' ';
352 $n++;
353 for (; ($n % 8) != 0; $n++) {
354 $res .= ' ';
356 next;
358 $res .= $c;
359 $n++;
362 return $res;
364 sub copy_spacing {
365 (my $res = shift) =~ tr/\t/ /c;
366 return $res;
369 sub line_stats {
370 my ($line) = @_;
372 # Drop the diff line leader and expand tabs
373 $line =~ s/^.//;
374 $line = expand_tabs($line);
376 # Pick the indent from the front of the line.
377 my ($white) = ($line =~ /^(\s*)/);
379 return (length($line), length($white));
382 my $sanitise_quote = '';
384 sub sanitise_line_reset {
385 my ($in_comment) = @_;
387 if ($in_comment) {
388 $sanitise_quote = '*/';
389 } else {
390 $sanitise_quote = '';
393 sub sanitise_line {
394 my ($line) = @_;
396 my $res = '';
397 my $l = '';
399 my $qlen = 0;
400 my $off = 0;
401 my $c;
403 # Always copy over the diff marker.
404 $res = substr($line, 0, 1);
406 for ($off = 1; $off < length($line); $off++) {
407 $c = substr($line, $off, 1);
409 # Comments we are wacking completly including the begin
410 # and end, all to $;.
411 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
412 $sanitise_quote = '*/';
414 substr($res, $off, 2, "$;$;");
415 $off++;
416 next;
418 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
419 $sanitise_quote = '';
420 substr($res, $off, 2, "$;$;");
421 $off++;
422 next;
424 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
425 $sanitise_quote = '//';
427 substr($res, $off, 2, $sanitise_quote);
428 $off++;
429 next;
432 # A \ in a string means ignore the next character.
433 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
434 $c eq "\\") {
435 substr($res, $off, 2, 'XX');
436 $off++;
437 next;
439 # Regular quotes.
440 if ($c eq "'" || $c eq '"') {
441 if ($sanitise_quote eq '') {
442 $sanitise_quote = $c;
444 substr($res, $off, 1, $c);
445 next;
446 } elsif ($sanitise_quote eq $c) {
447 $sanitise_quote = '';
451 #print "c<$c> SQ<$sanitise_quote>\n";
452 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
453 substr($res, $off, 1, $;);
454 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
455 substr($res, $off, 1, $;);
456 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
457 substr($res, $off, 1, 'X');
458 } else {
459 substr($res, $off, 1, $c);
463 if ($sanitise_quote eq '//') {
464 $sanitise_quote = '';
467 # The pathname on a #include may be surrounded by '<' and '>'.
468 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
469 my $clean = 'X' x length($1);
470 $res =~ s@\<.*\>@<$clean>@;
472 # The whole of a #error is a string.
473 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
474 my $clean = 'X' x length($1);
475 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
478 return $res;
481 sub ctx_statement_block {
482 my ($linenr, $remain, $off) = @_;
483 my $line = $linenr - 1;
484 my $blk = '';
485 my $soff = $off;
486 my $coff = $off - 1;
487 my $coff_set = 0;
489 my $loff = 0;
491 my $type = '';
492 my $level = 0;
493 my @stack = ();
494 my $p;
495 my $c;
496 my $len = 0;
498 my $remainder;
499 while (1) {
500 @stack = (['', 0]) if ($#stack == -1);
502 #warn "CSB: blk<$blk> remain<$remain>\n";
503 # If we are about to drop off the end, pull in more
504 # context.
505 if ($off >= $len) {
506 for (; $remain > 0; $line++) {
507 last if (!defined $lines[$line]);
508 next if ($lines[$line] =~ /^-/);
509 $remain--;
510 $loff = $len;
511 $blk .= $lines[$line] . "\n";
512 $len = length($blk);
513 $line++;
514 last;
516 # Bail if there is no further context.
517 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
518 if ($off >= $len) {
519 last;
522 $p = $c;
523 $c = substr($blk, $off, 1);
524 $remainder = substr($blk, $off);
526 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
528 # Handle nested #if/#else.
529 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
530 push(@stack, [ $type, $level ]);
531 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
532 ($type, $level) = @{$stack[$#stack - 1]};
533 } elsif ($remainder =~ /^#\s*endif\b/) {
534 ($type, $level) = @{pop(@stack)};
537 # Statement ends at the ';' or a close '}' at the
538 # outermost level.
539 if ($level == 0 && $c eq ';') {
540 last;
543 # An else is really a conditional as long as its not else if
544 if ($level == 0 && $coff_set == 0 &&
545 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
546 $remainder =~ /^(else)(?:\s|{)/ &&
547 $remainder !~ /^else\s+if\b/) {
548 $coff = $off + length($1) - 1;
549 $coff_set = 1;
550 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
551 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\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 && $coff < $soff) {
563 $coff = $off;
564 $coff_set = 1;
565 #warn "CSB: mark coff<$coff>\n";
568 if (($type eq '' || $type eq '{') && $c eq '{') {
569 $level++;
570 $type = '{';
572 if ($type eq '{' && $c eq '}') {
573 $level--;
574 $type = ($level != 0)? '{' : '';
576 if ($level == 0) {
577 if (substr($blk, $off + 1, 1) eq ';') {
578 $off++;
580 last;
583 $off++;
585 # We are truly at the end, so shuffle to the next line.
586 if ($off == $len) {
587 $loff = $len + 1;
588 $line++;
589 $remain--;
592 my $statement = substr($blk, $soff, $off - $soff + 1);
593 my $condition = substr($blk, $soff, $coff - $soff + 1);
595 #warn "STATEMENT<$statement>\n";
596 #warn "CONDITION<$condition>\n";
598 #print "coff<$coff> soff<$off> loff<$loff>\n";
600 return ($statement, $condition,
601 $line, $remain + 1, $off - $loff + 1, $level);
604 sub statement_lines {
605 my ($stmt) = @_;
607 # Strip the diff line prefixes and rip blank lines at start and end.
608 $stmt =~ s/(^|\n)./$1/g;
609 $stmt =~ s/^\s*//;
610 $stmt =~ s/\s*$//;
612 my @stmt_lines = ($stmt =~ /\n/g);
614 return $#stmt_lines + 2;
617 sub statement_rawlines {
618 my ($stmt) = @_;
620 my @stmt_lines = ($stmt =~ /\n/g);
622 return $#stmt_lines + 2;
625 sub statement_block_size {
626 my ($stmt) = @_;
628 $stmt =~ s/(^|\n)./$1/g;
629 $stmt =~ s/^\s*{//;
630 $stmt =~ s/}\s*$//;
631 $stmt =~ s/^\s*//;
632 $stmt =~ s/\s*$//;
634 my @stmt_lines = ($stmt =~ /\n/g);
635 my @stmt_statements = ($stmt =~ /;/g);
637 my $stmt_lines = $#stmt_lines + 2;
638 my $stmt_statements = $#stmt_statements + 1;
640 if ($stmt_lines > $stmt_statements) {
641 return $stmt_lines;
642 } else {
643 return $stmt_statements;
647 sub ctx_statement_full {
648 my ($linenr, $remain, $off) = @_;
649 my ($statement, $condition, $level);
651 my (@chunks);
653 # Grab the first conditional/block pair.
654 ($statement, $condition, $linenr, $remain, $off, $level) =
655 ctx_statement_block($linenr, $remain, $off);
656 #print "F: c<$condition> s<$statement> remain<$remain>\n";
657 push(@chunks, [ $condition, $statement ]);
658 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
659 return ($level, $linenr, @chunks);
662 # Pull in the following conditional/block pairs and see if they
663 # could continue the statement.
664 for (;;) {
665 ($statement, $condition, $linenr, $remain, $off, $level) =
666 ctx_statement_block($linenr, $remain, $off);
667 #print "C: c<$condition> s<$statement> remain<$remain>\n";
668 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
669 #print "C: push\n";
670 push(@chunks, [ $condition, $statement ]);
673 return ($level, $linenr, @chunks);
676 sub ctx_block_get {
677 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
678 my $line;
679 my $start = $linenr - 1;
680 my $blk = '';
681 my @o;
682 my @c;
683 my @res = ();
685 my $level = 0;
686 my @stack = ($level);
687 for ($line = $start; $remain > 0; $line++) {
688 next if ($rawlines[$line] =~ /^-/);
689 $remain--;
691 $blk .= $rawlines[$line];
693 # Handle nested #if/#else.
694 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
695 push(@stack, $level);
696 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
697 $level = $stack[$#stack - 1];
698 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
699 $level = pop(@stack);
702 foreach my $c (split(//, $lines[$line])) {
703 ##print "C<$c>L<$level><$open$close>O<$off>\n";
704 if ($off > 0) {
705 $off--;
706 next;
709 if ($c eq $close && $level > 0) {
710 $level--;
711 last if ($level == 0);
712 } elsif ($c eq $open) {
713 $level++;
717 if (!$outer || $level <= 1) {
718 push(@res, $rawlines[$line]);
721 last if ($level == 0);
724 return ($level, @res);
726 sub ctx_block_outer {
727 my ($linenr, $remain) = @_;
729 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
730 return @r;
732 sub ctx_block {
733 my ($linenr, $remain) = @_;
735 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
736 return @r;
738 sub ctx_statement {
739 my ($linenr, $remain, $off) = @_;
741 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
742 return @r;
744 sub ctx_block_level {
745 my ($linenr, $remain) = @_;
747 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
749 sub ctx_statement_level {
750 my ($linenr, $remain, $off) = @_;
752 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
755 sub ctx_locate_comment {
756 my ($first_line, $end_line) = @_;
758 # Catch a comment on the end of the line itself.
759 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
760 return $current_comment if (defined $current_comment);
762 # Look through the context and try and figure out if there is a
763 # comment.
764 my $in_comment = 0;
765 $current_comment = '';
766 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
767 my $line = $rawlines[$linenr - 1];
768 #warn " $line\n";
769 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
770 $in_comment = 1;
772 if ($line =~ m@/\*@) {
773 $in_comment = 1;
775 if (!$in_comment && $current_comment ne '') {
776 $current_comment = '';
778 $current_comment .= $line . "\n" if ($in_comment);
779 if ($line =~ m@\*/@) {
780 $in_comment = 0;
784 chomp($current_comment);
785 return($current_comment);
787 sub ctx_has_comment {
788 my ($first_line, $end_line) = @_;
789 my $cmt = ctx_locate_comment($first_line, $end_line);
791 ##print "LINE: $rawlines[$end_line - 1 ]\n";
792 ##print "CMMT: $cmt\n";
794 return ($cmt ne '');
797 sub raw_line {
798 my ($linenr, $cnt) = @_;
800 my $offset = $linenr - 1;
801 $cnt++;
803 my $line;
804 while ($cnt) {
805 $line = $rawlines[$offset++];
806 next if (defined($line) && $line =~ /^-/);
807 $cnt--;
810 return $line;
813 sub cat_vet {
814 my ($vet) = @_;
815 my ($res, $coded);
817 $res = '';
818 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
819 $res .= $1;
820 if ($2 ne '') {
821 $coded = sprintf("^%c", unpack('C', $2) + 64);
822 $res .= $coded;
825 $res =~ s/$/\$/;
827 return $res;
830 my $av_preprocessor = 0;
831 my $av_pending;
832 my @av_paren_type;
833 my $av_pend_colon;
835 sub annotate_reset {
836 $av_preprocessor = 0;
837 $av_pending = '_';
838 @av_paren_type = ('E');
839 $av_pend_colon = 'O';
842 sub annotate_values {
843 my ($stream, $type) = @_;
845 my $res;
846 my $var = '_' x length($stream);
847 my $cur = $stream;
849 print "$stream\n" if ($dbg_values > 1);
851 while (length($cur)) {
852 @av_paren_type = ('E') if ($#av_paren_type < 0);
853 print " <" . join('', @av_paren_type) .
854 "> <$type> <$av_pending>" if ($dbg_values > 1);
855 if ($cur =~ /^(\s+)/o) {
856 print "WS($1)\n" if ($dbg_values > 1);
857 if ($1 =~ /\n/ && $av_preprocessor) {
858 $type = pop(@av_paren_type);
859 $av_preprocessor = 0;
862 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) {
863 print "CAST($1)\n" if ($dbg_values > 1);
864 push(@av_paren_type, $type);
865 $type = 'C';
867 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
868 print "DECLARE($1)\n" if ($dbg_values > 1);
869 $type = 'T';
871 } elsif ($cur =~ /^($Modifier)\s*/) {
872 print "MODIFIER($1)\n" if ($dbg_values > 1);
873 $type = 'T';
875 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
876 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
877 $av_preprocessor = 1;
878 push(@av_paren_type, $type);
879 if ($2 ne '') {
880 $av_pending = 'N';
882 $type = 'E';
884 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
885 print "UNDEF($1)\n" if ($dbg_values > 1);
886 $av_preprocessor = 1;
887 push(@av_paren_type, $type);
889 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
890 print "PRE_START($1)\n" if ($dbg_values > 1);
891 $av_preprocessor = 1;
893 push(@av_paren_type, $type);
894 push(@av_paren_type, $type);
895 $type = 'E';
897 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
898 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
899 $av_preprocessor = 1;
901 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
903 $type = 'E';
905 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
906 print "PRE_END($1)\n" if ($dbg_values > 1);
908 $av_preprocessor = 1;
910 # Assume all arms of the conditional end as this
911 # one does, and continue as if the #endif was not here.
912 pop(@av_paren_type);
913 push(@av_paren_type, $type);
914 $type = 'E';
916 } elsif ($cur =~ /^(\\\n)/o) {
917 print "PRECONT($1)\n" if ($dbg_values > 1);
919 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
920 print "ATTR($1)\n" if ($dbg_values > 1);
921 $av_pending = $type;
922 $type = 'N';
924 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
925 print "SIZEOF($1)\n" if ($dbg_values > 1);
926 if (defined $2) {
927 $av_pending = 'V';
929 $type = 'N';
931 } elsif ($cur =~ /^(if|while|for)\b/o) {
932 print "COND($1)\n" if ($dbg_values > 1);
933 $av_pending = 'E';
934 $type = 'N';
936 } elsif ($cur =~/^(case)/o) {
937 print "CASE($1)\n" if ($dbg_values > 1);
938 $av_pend_colon = 'C';
939 $type = 'N';
941 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
942 print "KEYWORD($1)\n" if ($dbg_values > 1);
943 $type = 'N';
945 } elsif ($cur =~ /^(\()/o) {
946 print "PAREN('$1')\n" if ($dbg_values > 1);
947 push(@av_paren_type, $av_pending);
948 $av_pending = '_';
949 $type = 'N';
951 } elsif ($cur =~ /^(\))/o) {
952 my $new_type = pop(@av_paren_type);
953 if ($new_type ne '_') {
954 $type = $new_type;
955 print "PAREN('$1') -> $type\n"
956 if ($dbg_values > 1);
957 } else {
958 print "PAREN('$1')\n" if ($dbg_values > 1);
961 } elsif ($cur =~ /^($Ident)\s*\(/o) {
962 print "FUNC($1)\n" if ($dbg_values > 1);
963 $type = 'V';
964 $av_pending = 'V';
966 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
967 if (defined $2 && $type eq 'C' || $type eq 'T') {
968 $av_pend_colon = 'B';
969 } elsif ($type eq 'E') {
970 $av_pend_colon = 'L';
972 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
973 $type = 'V';
975 } elsif ($cur =~ /^($Ident|$Constant)/o) {
976 print "IDENT($1)\n" if ($dbg_values > 1);
977 $type = 'V';
979 } elsif ($cur =~ /^($Assignment)/o) {
980 print "ASSIGN($1)\n" if ($dbg_values > 1);
981 $type = 'N';
983 } elsif ($cur =~/^(;|{|})/) {
984 print "END($1)\n" if ($dbg_values > 1);
985 $type = 'E';
986 $av_pend_colon = 'O';
988 } elsif ($cur =~/^(,)/) {
989 print "COMMA($1)\n" if ($dbg_values > 1);
990 $type = 'C';
992 } elsif ($cur =~ /^(\?)/o) {
993 print "QUESTION($1)\n" if ($dbg_values > 1);
994 $type = 'N';
996 } elsif ($cur =~ /^(:)/o) {
997 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
999 substr($var, length($res), 1, $av_pend_colon);
1000 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1001 $type = 'E';
1002 } else {
1003 $type = 'N';
1005 $av_pend_colon = 'O';
1007 } elsif ($cur =~ /^(\[)/o) {
1008 print "CLOSE($1)\n" if ($dbg_values > 1);
1009 $type = 'N';
1011 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1012 my $variant;
1014 print "OPV($1)\n" if ($dbg_values > 1);
1015 if ($type eq 'V') {
1016 $variant = 'B';
1017 } else {
1018 $variant = 'U';
1021 substr($var, length($res), 1, $variant);
1022 $type = 'N';
1024 } elsif ($cur =~ /^($Operators)/o) {
1025 print "OP($1)\n" if ($dbg_values > 1);
1026 if ($1 ne '++' && $1 ne '--') {
1027 $type = 'N';
1030 } elsif ($cur =~ /(^.)/o) {
1031 print "C($1)\n" if ($dbg_values > 1);
1033 if (defined $1) {
1034 $cur = substr($cur, length($1));
1035 $res .= $type x length($1);
1039 return ($res, $var);
1042 sub possible {
1043 my ($possible, $line) = @_;
1044 my $notPermitted = qr{(?:
1045 ^(?:
1046 $Modifier|
1047 $Storage|
1048 $Type|
1049 DEFINE_\S+
1051 ^(?:
1052 goto|
1053 return|
1054 case|
1055 else|
1056 asm|__asm__|
1058 )(?:\s|$)|
1059 ^(?:typedef|struct|enum)\b
1060 )}x;
1061 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1062 if ($possible !~ $notPermitted) {
1063 # Check for modifiers.
1064 $possible =~ s/\s*$Storage\s*//g;
1065 $possible =~ s/\s*$Sparse\s*//g;
1066 if ($possible =~ /^\s*$/) {
1068 } elsif ($possible =~ /\s/) {
1069 $possible =~ s/\s*$Type\s*//g;
1070 for my $modifier (split(' ', $possible)) {
1071 if ($modifier !~ $notPermitted) {
1072 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1073 push(@modifierList, $modifier);
1077 } else {
1078 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1079 push(@typeList, $possible);
1081 build_types();
1082 } else {
1083 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1087 my $prefix = '';
1089 sub report {
1090 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1091 return 0;
1093 my $line = $prefix . $_[0];
1095 $line = (split('\n', $line))[0] . "\n" if ($terse);
1097 push(our @report, $line);
1099 return 1;
1101 sub report_dump {
1102 our @report;
1104 sub ERROR {
1105 if (report("ERROR: $_[0]\n")) {
1106 our $clean = 0;
1107 our $cnt_error++;
1110 sub WARN {
1111 if (report("WARNING: $_[0]\n")) {
1112 our $clean = 0;
1113 our $cnt_warn++;
1116 sub CHK {
1117 if ($check && report("CHECK: $_[0]\n")) {
1118 our $clean = 0;
1119 our $cnt_chk++;
1123 sub check_absolute_file {
1124 my ($absolute, $herecurr) = @_;
1125 my $file = $absolute;
1127 ##print "absolute<$absolute>\n";
1129 # See if any suffix of this path is a path within the tree.
1130 while ($file =~ s@^[^/]*/@@) {
1131 if (-f "$root/$file") {
1132 ##print "file<$file>\n";
1133 last;
1136 if (! -f _) {
1137 return 0;
1140 # It is, so see if the prefix is acceptable.
1141 my $prefix = $absolute;
1142 substr($prefix, -length($file)) = '';
1144 ##print "prefix<$prefix>\n";
1145 if ($prefix ne ".../") {
1146 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1150 sub process {
1151 my $filename = shift;
1153 my $linenr=0;
1154 my $prevline="";
1155 my $prevrawline="";
1156 my $stashline="";
1157 my $stashrawline="";
1159 my $length;
1160 my $indent;
1161 my $previndent=0;
1162 my $stashindent=0;
1164 our $clean = 1;
1165 my $signoff = 0;
1166 my $is_patch = 0;
1168 our @report = ();
1169 our $cnt_lines = 0;
1170 our $cnt_error = 0;
1171 our $cnt_warn = 0;
1172 our $cnt_chk = 0;
1174 # Trace the real file/line as we go.
1175 my $realfile = '';
1176 my $realline = 0;
1177 my $realcnt = 0;
1178 my $here = '';
1179 my $in_comment = 0;
1180 my $comment_edge = 0;
1181 my $first_line = 0;
1182 my $p1_prefix = '';
1184 my $prev_values = 'E';
1186 # suppression flags
1187 my %suppress_ifbraces;
1188 my %suppress_whiletrailers;
1189 my %suppress_export;
1191 # Pre-scan the patch sanitizing the lines.
1192 # Pre-scan the patch looking for any __setup documentation.
1194 my @setup_docs = ();
1195 my $setup_docs = 0;
1197 sanitise_line_reset();
1198 my $line;
1199 foreach my $rawline (@rawlines) {
1200 $linenr++;
1201 $line = $rawline;
1203 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1204 $setup_docs = 0;
1205 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1206 $setup_docs = 1;
1208 #next;
1210 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1211 $realline=$1-1;
1212 if (defined $2) {
1213 $realcnt=$3+1;
1214 } else {
1215 $realcnt=1+1;
1217 $in_comment = 0;
1219 # Guestimate if this is a continuing comment. Run
1220 # the context looking for a comment "edge". If this
1221 # edge is a close comment then we must be in a comment
1222 # at context start.
1223 my $edge;
1224 my $cnt = $realcnt;
1225 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1226 next if (defined $rawlines[$ln - 1] &&
1227 $rawlines[$ln - 1] =~ /^-/);
1228 $cnt--;
1229 #print "RAW<$rawlines[$ln - 1]>\n";
1230 last if (!defined $rawlines[$ln - 1]);
1231 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1232 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1233 ($edge) = $1;
1234 last;
1237 if (defined $edge && $edge eq '*/') {
1238 $in_comment = 1;
1241 # Guestimate if this is a continuing comment. If this
1242 # is the start of a diff block and this line starts
1243 # ' *' then it is very likely a comment.
1244 if (!defined $edge &&
1245 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1247 $in_comment = 1;
1250 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1251 sanitise_line_reset($in_comment);
1253 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1254 # Standardise the strings and chars within the input to
1255 # simplify matching -- only bother with positive lines.
1256 $line = sanitise_line($rawline);
1258 push(@lines, $line);
1260 if ($realcnt > 1) {
1261 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1262 } else {
1263 $realcnt = 0;
1266 #print "==>$rawline\n";
1267 #print "-->$line\n";
1269 if ($setup_docs && $line =~ /^\+/) {
1270 push(@setup_docs, $line);
1274 $prefix = '';
1276 $realcnt = 0;
1277 $linenr = 0;
1278 foreach my $line (@lines) {
1279 $linenr++;
1281 my $rawline = $rawlines[$linenr - 1];
1283 #extract the line range in the file after the patch is applied
1284 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1285 $is_patch = 1;
1286 $first_line = $linenr + 1;
1287 $realline=$1-1;
1288 if (defined $2) {
1289 $realcnt=$3+1;
1290 } else {
1291 $realcnt=1+1;
1293 annotate_reset();
1294 $prev_values = 'E';
1296 %suppress_ifbraces = ();
1297 %suppress_whiletrailers = ();
1298 %suppress_export = ();
1299 next;
1301 # track the line number as we move through the hunk, note that
1302 # new versions of GNU diff omit the leading space on completely
1303 # blank context lines so we need to count that too.
1304 } elsif ($line =~ /^( |\+|$)/) {
1305 $realline++;
1306 $realcnt-- if ($realcnt != 0);
1308 # Measure the line length and indent.
1309 ($length, $indent) = line_stats($rawline);
1311 # Track the previous line.
1312 ($prevline, $stashline) = ($stashline, $line);
1313 ($previndent, $stashindent) = ($stashindent, $indent);
1314 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1316 #warn "line<$line>\n";
1318 } elsif ($realcnt == 1) {
1319 $realcnt--;
1322 my $hunk_line = ($realcnt != 0);
1324 #make up the handle for any error we report on this line
1325 $prefix = "$filename:$realline: " if ($emacs && $file);
1326 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1328 $here = "#$linenr: " if (!$file);
1329 $here = "#$realline: " if ($file);
1331 # extract the filename as it passes
1332 if ($line =~ /^diff --git.*?(\S+)$/) {
1333 $realfile = $1;
1334 $realfile =~ s@^([^/]*)/@@;
1336 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1337 $realfile = $1;
1338 $realfile =~ s@^([^/]*)/@@;
1340 $p1_prefix = $1;
1341 if (!$file && $tree && $p1_prefix ne '' &&
1342 -e "$root/$p1_prefix") {
1343 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1346 if ($realfile =~ m@^include/asm/@) {
1347 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1349 next;
1352 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1354 my $hereline = "$here\n$rawline\n";
1355 my $herecurr = "$here\n$rawline\n";
1356 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1358 $cnt_lines++ if ($realcnt != 0);
1360 # Check for incorrect file permissions
1361 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1362 my $permhere = $here . "FILE: $realfile\n";
1363 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1364 ERROR("do not set execute permissions for source files\n" . $permhere);
1368 #check the patch for a signoff:
1369 if ($line =~ /^\s*signed-off-by:/i) {
1370 # This is a signoff, if ugly, so do not double report.
1371 $signoff++;
1372 if (!($line =~ /^\s*Signed-off-by:/)) {
1373 WARN("Signed-off-by: is the preferred form\n" .
1374 $herecurr);
1376 if ($line =~ /^\s*signed-off-by:\S/i) {
1377 WARN("space required after Signed-off-by:\n" .
1378 $herecurr);
1382 # Check for wrappage within a valid hunk of the file
1383 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1384 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1385 $herecurr) if (!$emitted_corrupt++);
1388 # Check for absolute kernel paths.
1389 if ($tree) {
1390 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1391 my $file = $1;
1393 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1394 check_absolute_file($1, $herecurr)) {
1396 } else {
1397 check_absolute_file($file, $herecurr);
1402 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1403 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1404 $rawline !~ m/^$UTF8*$/) {
1405 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1407 my $blank = copy_spacing($rawline);
1408 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1409 my $hereptr = "$hereline$ptr\n";
1411 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1414 # ignore non-hunk lines and lines being removed
1415 next if (!$hunk_line || $line =~ /^-/);
1417 #trailing whitespace
1418 if ($line =~ /^\+.*\015/) {
1419 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1420 ERROR("DOS line endings\n" . $herevet);
1422 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1423 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1424 ERROR("trailing whitespace\n" . $herevet);
1425 $rpt_cleaners = 1;
1428 # check for Kconfig help text having a real description
1429 # Only applies when adding the entry originally, after that we do not have
1430 # sufficient context to determine whether it is indeed long enough.
1431 if ($realfile =~ /Kconfig/ &&
1432 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1433 my $length = 0;
1434 my $cnt = $realcnt;
1435 my $ln = $linenr + 1;
1436 my $f;
1437 my $is_end = 0;
1438 while ($cnt > 0 && defined $lines[$ln - 1]) {
1439 $f = $lines[$ln - 1];
1440 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1441 $is_end = $lines[$ln - 1] =~ /^\+/;
1442 $ln++;
1444 next if ($f =~ /^-/);
1445 $f =~ s/^.//;
1446 $f =~ s/#.*//;
1447 $f =~ s/^\s+//;
1448 next if ($f =~ /^$/);
1449 if ($f =~ /^\s*config\s/) {
1450 $is_end = 1;
1451 last;
1453 $length++;
1455 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1456 #print "is_end<$is_end> length<$length>\n";
1459 # check we are in a valid source file if not then ignore this hunk
1460 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1462 #80 column limit
1463 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1464 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1465 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1466 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1467 $length > 80)
1469 WARN("line over 80 characters\n" . $herecurr);
1472 # check for spaces before a quoted newline
1473 if ($rawline =~ /^.*\".*\s\\n/) {
1474 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1477 # check for adding lines without a newline.
1478 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1479 WARN("adding a line without newline at end of file\n" . $herecurr);
1482 # Blackfin: use hi/lo macros
1483 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1484 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1485 my $herevet = "$here\n" . cat_vet($line) . "\n";
1486 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1488 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1489 my $herevet = "$here\n" . cat_vet($line) . "\n";
1490 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1494 # check we are in a valid source file C or perl if not then ignore this hunk
1495 next if ($realfile !~ /\.(h|c|pl)$/);
1497 # in QEMU, no tabs are allowed
1498 if ($rawline =~ /^\+.*\t/) {
1499 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1500 ERROR("code indent should never use tabs\n" . $herevet);
1501 $rpt_cleaners = 1;
1504 # check we are in a valid C source file if not then ignore this hunk
1505 next if ($realfile !~ /\.(h|c)$/);
1507 # check for RCS/CVS revision markers
1508 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1509 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1512 # Blackfin: don't use __builtin_bfin_[cs]sync
1513 if ($line =~ /__builtin_bfin_csync/) {
1514 my $herevet = "$here\n" . cat_vet($line) . "\n";
1515 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1517 if ($line =~ /__builtin_bfin_ssync/) {
1518 my $herevet = "$here\n" . cat_vet($line) . "\n";
1519 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1522 # Check for potential 'bare' types
1523 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1524 $realline_next);
1525 if ($realcnt && $line =~ /.\s*\S/) {
1526 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1527 ctx_statement_block($linenr, $realcnt, 0);
1528 $stat =~ s/\n./\n /g;
1529 $cond =~ s/\n./\n /g;
1531 # Find the real next line.
1532 $realline_next = $line_nr_next;
1533 if (defined $realline_next &&
1534 (!defined $lines[$realline_next - 1] ||
1535 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1536 $realline_next++;
1539 my $s = $stat;
1540 $s =~ s/{.*$//s;
1542 # Ignore goto labels.
1543 if ($s =~ /$Ident:\*$/s) {
1545 # Ignore functions being called
1546 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1548 } elsif ($s =~ /^.\s*else\b/s) {
1550 # declarations always start with types
1551 } 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) {
1552 my $type = $1;
1553 $type =~ s/\s+/ /g;
1554 possible($type, "A:" . $s);
1556 # definitions in global scope can only start with types
1557 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1558 possible($1, "B:" . $s);
1561 # any (foo ... *) is a pointer cast, and foo is a type
1562 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1563 possible($1, "C:" . $s);
1566 # Check for any sort of function declaration.
1567 # int foo(something bar, other baz);
1568 # void (*store_gdt)(x86_descr_ptr *);
1569 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1570 my ($name_len) = length($1);
1572 my $ctx = $s;
1573 substr($ctx, 0, $name_len + 1, '');
1574 $ctx =~ s/\)[^\)]*$//;
1576 for my $arg (split(/\s*,\s*/, $ctx)) {
1577 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1579 possible($1, "D:" . $s);
1587 # Checks which may be anchored in the context.
1590 # Check for switch () and associated case and default
1591 # statements should be at the same indent.
1592 if ($line=~/\bswitch\s*\(.*\)/) {
1593 my $err = '';
1594 my $sep = '';
1595 my @ctx = ctx_block_outer($linenr, $realcnt);
1596 shift(@ctx);
1597 for my $ctx (@ctx) {
1598 my ($clen, $cindent) = line_stats($ctx);
1599 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1600 $indent != $cindent) {
1601 $err .= "$sep$ctx\n";
1602 $sep = '';
1603 } else {
1604 $sep = "[...]\n";
1607 if ($err ne '') {
1608 ERROR("switch and case should be at the same indent\n$hereline$err");
1612 # if/while/etc brace do not go on next line, unless defining a do while loop,
1613 # or if that brace on the next line is for something else
1614 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1615 my $pre_ctx = "$1$2";
1617 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1618 my $ctx_cnt = $realcnt - $#ctx - 1;
1619 my $ctx = join("\n", @ctx);
1621 my $ctx_ln = $linenr;
1622 my $ctx_skip = $realcnt;
1624 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1625 defined $lines[$ctx_ln - 1] &&
1626 $lines[$ctx_ln - 1] =~ /^-/)) {
1627 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1628 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1629 $ctx_ln++;
1632 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1633 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1635 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1636 ERROR("that open brace { should be on the previous line\n" .
1637 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1639 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1640 $ctx =~ /\)\s*\;\s*$/ &&
1641 defined $lines[$ctx_ln - 1])
1643 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1644 if ($nindent > $indent) {
1645 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1646 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1651 # Check relative indent for conditionals and blocks.
1652 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1653 my ($s, $c) = ($stat, $cond);
1655 substr($s, 0, length($c), '');
1657 # Make sure we remove the line prefixes as we have
1658 # none on the first line, and are going to readd them
1659 # where necessary.
1660 $s =~ s/\n./\n/gs;
1662 # Find out how long the conditional actually is.
1663 my @newlines = ($c =~ /\n/gs);
1664 my $cond_lines = 1 + $#newlines;
1666 # We want to check the first line inside the block
1667 # starting at the end of the conditional, so remove:
1668 # 1) any blank line termination
1669 # 2) any opening brace { on end of the line
1670 # 3) any do (...) {
1671 my $continuation = 0;
1672 my $check = 0;
1673 $s =~ s/^.*\bdo\b//;
1674 $s =~ s/^\s*{//;
1675 if ($s =~ s/^\s*\\//) {
1676 $continuation = 1;
1678 if ($s =~ s/^\s*?\n//) {
1679 $check = 1;
1680 $cond_lines++;
1683 # Also ignore a loop construct at the end of a
1684 # preprocessor statement.
1685 if (($prevline =~ /^.\s*#\s*define\s/ ||
1686 $prevline =~ /\\\s*$/) && $continuation == 0) {
1687 $check = 0;
1690 my $cond_ptr = -1;
1691 $continuation = 0;
1692 while ($cond_ptr != $cond_lines) {
1693 $cond_ptr = $cond_lines;
1695 # If we see an #else/#elif then the code
1696 # is not linear.
1697 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1698 $check = 0;
1701 # Ignore:
1702 # 1) blank lines, they should be at 0,
1703 # 2) preprocessor lines, and
1704 # 3) labels.
1705 if ($continuation ||
1706 $s =~ /^\s*?\n/ ||
1707 $s =~ /^\s*#\s*?/ ||
1708 $s =~ /^\s*$Ident\s*:/) {
1709 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1710 if ($s =~ s/^.*?\n//) {
1711 $cond_lines++;
1716 my (undef, $sindent) = line_stats("+" . $s);
1717 my $stat_real = raw_line($linenr, $cond_lines);
1719 # Check if either of these lines are modified, else
1720 # this is not this patch's fault.
1721 if (!defined($stat_real) ||
1722 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1723 $check = 0;
1725 if (defined($stat_real) && $cond_lines > 1) {
1726 $stat_real = "[...]\n$stat_real";
1729 #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";
1731 if ($check && (($sindent % 4) != 0 ||
1732 ($sindent <= $indent && $s ne ''))) {
1733 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1737 # Track the 'values' across context and added lines.
1738 my $opline = $line; $opline =~ s/^./ /;
1739 my ($curr_values, $curr_vars) =
1740 annotate_values($opline . "\n", $prev_values);
1741 $curr_values = $prev_values . $curr_values;
1742 if ($dbg_values) {
1743 my $outline = $opline; $outline =~ s/\t/ /g;
1744 print "$linenr > .$outline\n";
1745 print "$linenr > $curr_values\n";
1746 print "$linenr > $curr_vars\n";
1748 $prev_values = substr($curr_values, -1);
1750 #ignore lines not being added
1751 if ($line=~/^[^\+]/) {next;}
1753 # TEST: allow direct testing of the type matcher.
1754 if ($dbg_type) {
1755 if ($line =~ /^.\s*$Declare\s*$/) {
1756 ERROR("TEST: is type\n" . $herecurr);
1757 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1758 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1760 next;
1762 # TEST: allow direct testing of the attribute matcher.
1763 if ($dbg_attr) {
1764 if ($line =~ /^.\s*$Modifier\s*$/) {
1765 ERROR("TEST: is attr\n" . $herecurr);
1766 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1767 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1769 next;
1772 # check for initialisation to aggregates open brace on the next line
1773 if ($line =~ /^.\s*{/ &&
1774 $prevline =~ /(?:^|[^=])=\s*$/) {
1775 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1779 # Checks which are anchored on the added line.
1782 # check for malformed paths in #include statements (uses RAW line)
1783 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1784 my $path = $1;
1785 if ($path =~ m{//}) {
1786 ERROR("malformed #include filename\n" .
1787 $herecurr);
1791 # no C99 // comments
1792 if ($line =~ m{//}) {
1793 ERROR("do not use C99 // comments\n" . $herecurr);
1795 # Remove C99 comments.
1796 $line =~ s@//.*@@;
1797 $opline =~ s@//.*@@;
1799 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1800 # the whole statement.
1801 #print "APW <$lines[$realline_next - 1]>\n";
1802 if (defined $realline_next &&
1803 exists $lines[$realline_next - 1] &&
1804 !defined $suppress_export{$realline_next} &&
1805 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1806 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1807 # Handle definitions which produce identifiers with
1808 # a prefix:
1809 # XXX(foo);
1810 # EXPORT_SYMBOL(something_foo);
1811 my $name = $1;
1812 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1813 $name =~ /^${Ident}_$2/) {
1814 #print "FOO C name<$name>\n";
1815 $suppress_export{$realline_next} = 1;
1817 } elsif ($stat !~ /(?:
1818 \n.}\s*$|
1819 ^.DEFINE_$Ident\(\Q$name\E\)|
1820 ^.DECLARE_$Ident\(\Q$name\E\)|
1821 ^.LIST_HEAD\(\Q$name\E\)|
1822 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1823 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1824 )/x) {
1825 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1826 $suppress_export{$realline_next} = 2;
1827 } else {
1828 $suppress_export{$realline_next} = 1;
1831 if (!defined $suppress_export{$linenr} &&
1832 $prevline =~ /^.\s*$/ &&
1833 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1834 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1835 #print "FOO B <$lines[$linenr - 1]>\n";
1836 $suppress_export{$linenr} = 2;
1838 if (defined $suppress_export{$linenr} &&
1839 $suppress_export{$linenr} == 2) {
1840 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1843 # check for global initialisers.
1844 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1845 ERROR("do not initialise globals to 0 or NULL\n" .
1846 $herecurr);
1848 # check for static initialisers.
1849 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1850 ERROR("do not initialise statics to 0 or NULL\n" .
1851 $herecurr);
1854 # * goes on variable not on type
1855 # (char*[ const])
1856 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1857 my ($from, $to) = ($1, $1);
1859 # Should start with a space.
1860 $to =~ s/^(\S)/ $1/;
1861 # Should not end with a space.
1862 $to =~ s/\s+$//;
1863 # '*'s should not have spaces between.
1864 while ($to =~ s/\*\s+\*/\*\*/) {
1867 #print "from<$from> to<$to>\n";
1868 if ($from ne $to) {
1869 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1871 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1872 my ($from, $to, $ident) = ($1, $1, $2);
1874 # Should start with a space.
1875 $to =~ s/^(\S)/ $1/;
1876 # Should not end with a space.
1877 $to =~ s/\s+$//;
1878 # '*'s should not have spaces between.
1879 while ($to =~ s/\*\s+\*/\*\*/) {
1881 # Modifiers should have spaces.
1882 $to =~ s/(\b$Modifier$)/$1 /;
1884 #print "from<$from> to<$to> ident<$ident>\n";
1885 if ($from ne $to && $ident !~ /^$Modifier$/) {
1886 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1890 # # no BUG() or BUG_ON()
1891 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1892 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1893 # print "$herecurr";
1894 # $clean = 0;
1897 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1898 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1901 # printk should use KERN_* levels. Note that follow on printk's on the
1902 # same line do not need a level, so we use the current block context
1903 # to try and find and validate the current printk. In summary the current
1904 # printk includes all preceeding printk's which have no newline on the end.
1905 # we assume the first bad printk is the one to report.
1906 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1907 my $ok = 0;
1908 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1909 #print "CHECK<$lines[$ln - 1]\n";
1910 # we have a preceeding printk if it ends
1911 # with "\n" ignore it, else it is to blame
1912 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1913 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1914 $ok = 1;
1916 last;
1919 if ($ok == 0) {
1920 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1924 # function brace can't be on same line, except for #defines of do while,
1925 # or if closed on same line
1926 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1927 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1928 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1931 # open braces for enum, union and struct go on the same line.
1932 if ($line =~ /^.\s*{/ &&
1933 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1934 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1937 # missing space after union, struct or enum definition
1938 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1939 WARN("missing space after $1 definition\n" . $herecurr);
1942 # check for spacing round square brackets; allowed:
1943 # 1. with a type on the left -- int [] a;
1944 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1945 # 3. inside a curly brace -- = { [0...10] = 5 }
1946 while ($line =~ /(.*?\s)\[/g) {
1947 my ($where, $prefix) = ($-[1], $1);
1948 if ($prefix !~ /$Type\s+$/ &&
1949 ($where != 0 || $prefix !~ /^.\s+$/) &&
1950 $prefix !~ /{\s+$/) {
1951 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1955 # check for spaces between functions and their parentheses.
1956 while ($line =~ /($Ident)\s+\(/g) {
1957 my $name = $1;
1958 my $ctx_before = substr($line, 0, $-[1]);
1959 my $ctx = "$ctx_before$name";
1961 # Ignore those directives where spaces _are_ permitted.
1962 if ($name =~ /^(?:
1963 if|for|while|switch|return|case|
1964 volatile|__volatile__|
1965 __attribute__|format|__extension__|
1966 asm|__asm__)$/x)
1969 # cpp #define statements have non-optional spaces, ie
1970 # if there is a space between the name and the open
1971 # parenthesis it is simply not a parameter group.
1972 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1974 # cpp #elif statement condition may start with a (
1975 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1977 # If this whole things ends with a type its most
1978 # likely a typedef for a function.
1979 } elsif ($ctx =~ /$Type$/) {
1981 } else {
1982 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1985 # Check operator spacing.
1986 if (!($line=~/\#\s*include/)) {
1987 my $ops = qr{
1988 <<=|>>=|<=|>=|==|!=|
1989 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1990 =>|->|<<|>>|<|>|=|!|~|
1991 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1992 \?|:
1994 my @elements = split(/($ops|;)/, $opline);
1995 my $off = 0;
1997 my $blank = copy_spacing($opline);
1999 for (my $n = 0; $n < $#elements; $n += 2) {
2000 $off += length($elements[$n]);
2002 # Pick up the preceeding and succeeding characters.
2003 my $ca = substr($opline, 0, $off);
2004 my $cc = '';
2005 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2006 $cc = substr($opline, $off + length($elements[$n + 1]));
2008 my $cb = "$ca$;$cc";
2010 my $a = '';
2011 $a = 'V' if ($elements[$n] ne '');
2012 $a = 'W' if ($elements[$n] =~ /\s$/);
2013 $a = 'C' if ($elements[$n] =~ /$;$/);
2014 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2015 $a = 'O' if ($elements[$n] eq '');
2016 $a = 'E' if ($ca =~ /^\s*$/);
2018 my $op = $elements[$n + 1];
2020 my $c = '';
2021 if (defined $elements[$n + 2]) {
2022 $c = 'V' if ($elements[$n + 2] ne '');
2023 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2024 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2025 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2026 $c = 'O' if ($elements[$n + 2] eq '');
2027 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2028 } else {
2029 $c = 'E';
2032 my $ctx = "${a}x${c}";
2034 my $at = "(ctx:$ctx)";
2036 my $ptr = substr($blank, 0, $off) . "^";
2037 my $hereptr = "$hereline$ptr\n";
2039 # Pull out the value of this operator.
2040 my $op_type = substr($curr_values, $off + 1, 1);
2042 # Get the full operator variant.
2043 my $opv = $op . substr($curr_vars, $off, 1);
2045 # Ignore operators passed as parameters.
2046 if ($op_type ne 'V' &&
2047 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2049 # # Ignore comments
2050 # } elsif ($op =~ /^$;+$/) {
2052 # ; should have either the end of line or a space or \ after it
2053 } elsif ($op eq ';') {
2054 if ($ctx !~ /.x[WEBC]/ &&
2055 $cc !~ /^\\/ && $cc !~ /^;/) {
2056 ERROR("space required after that '$op' $at\n" . $hereptr);
2059 # // is a comment
2060 } elsif ($op eq '//') {
2062 # No spaces for:
2063 # ->
2064 # : when part of a bitfield
2065 } elsif ($op eq '->' || $opv eq ':B') {
2066 if ($ctx =~ /Wx.|.xW/) {
2067 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2070 # , must have a space on the right.
2071 # not required when having a single },{ on one line
2072 } elsif ($op eq ',') {
2073 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2074 ($elements[$n] . $elements[$n + 2]) !~ " *}{") {
2075 ERROR("space required after that '$op' $at\n" . $hereptr);
2078 # '*' as part of a type definition -- reported already.
2079 } elsif ($opv eq '*_') {
2080 #warn "'*' is part of type\n";
2082 # unary operators should have a space before and
2083 # none after. May be left adjacent to another
2084 # unary operator, or a cast
2085 } elsif ($op eq '!' || $op eq '~' ||
2086 $opv eq '*U' || $opv eq '-U' ||
2087 $opv eq '&U' || $opv eq '&&U') {
2088 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2089 ERROR("space required before that '$op' $at\n" . $hereptr);
2091 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2092 # A unary '*' may be const
2094 } elsif ($ctx =~ /.xW/) {
2095 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2098 # unary ++ and unary -- are allowed no space on one side.
2099 } elsif ($op eq '++' or $op eq '--') {
2100 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2101 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2103 if ($ctx =~ /Wx[BE]/ ||
2104 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2105 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2107 if ($ctx =~ /ExW/) {
2108 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2112 # << and >> may either have or not have spaces both sides
2113 } elsif ($op eq '<<' or $op eq '>>' or
2114 $op eq '&' or $op eq '^' or $op eq '|' or
2115 $op eq '+' or $op eq '-' or
2116 $op eq '*' or $op eq '/' or
2117 $op eq '%')
2119 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2120 ERROR("need consistent spacing around '$op' $at\n" .
2121 $hereptr);
2124 # A colon needs no spaces before when it is
2125 # terminating a case value or a label.
2126 } elsif ($opv eq ':C' || $opv eq ':L') {
2127 if ($ctx =~ /Wx./) {
2128 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2131 # All the others need spaces both sides.
2132 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2133 my $ok = 0;
2135 # Ignore email addresses <foo@bar>
2136 if (($op eq '<' &&
2137 $cc =~ /^\S+\@\S+>/) ||
2138 ($op eq '>' &&
2139 $ca =~ /<\S+\@\S+$/))
2141 $ok = 1;
2144 # Ignore ?:
2145 if (($opv eq ':O' && $ca =~ /\?$/) ||
2146 ($op eq '?' && $cc =~ /^:/)) {
2147 $ok = 1;
2150 if ($ok == 0) {
2151 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2154 $off += length($elements[$n + 1]);
2158 # check for multiple assignments
2159 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2160 CHK("multiple assignments should be avoided\n" . $herecurr);
2163 ## # check for multiple declarations, allowing for a function declaration
2164 ## # continuation.
2165 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2166 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2168 ## # Remove any bracketed sections to ensure we do not
2169 ## # falsly report the parameters of functions.
2170 ## my $ln = $line;
2171 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2172 ## }
2173 ## if ($ln =~ /,/) {
2174 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2175 ## }
2176 ## }
2178 #need space before brace following if, while, etc
2179 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2180 $line =~ /do{/) {
2181 ERROR("space required before the open brace '{'\n" . $herecurr);
2184 # closing brace should have a space following it when it has anything
2185 # on the line
2186 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2187 ERROR("space required after that close brace '}'\n" . $herecurr);
2190 # check spacing on square brackets
2191 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2192 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2194 if ($line =~ /\s\]/) {
2195 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2198 # check spacing on parentheses
2199 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2200 $line !~ /for\s*\(\s+;/) {
2201 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2203 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2204 $line !~ /for\s*\(.*;\s+\)/ &&
2205 $line !~ /:\s+\)/) {
2206 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2209 #goto labels aren't indented, allow a single space however
2210 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2211 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2212 WARN("labels should not be indented\n" . $herecurr);
2215 # Return is not a function.
2216 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2217 my $spacing = $1;
2218 my $value = $2;
2220 # Flatten any parentheses
2221 $value =~ s/\(/ \(/g;
2222 $value =~ s/\)/\) /g;
2223 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2224 $value !~ /(?:$Ident|-?$Constant)\s*
2225 $Compare\s*
2226 (?:$Ident|-?$Constant)/x &&
2227 $value =~ s/\([^\(\)]*\)/1/) {
2229 #print "value<$value>\n";
2230 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2231 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2233 } elsif ($spacing !~ /\s+/) {
2234 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2237 # Return of what appears to be an errno should normally be -'ve
2238 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2239 my $name = $1;
2240 if ($name ne 'EOF' && $name ne 'ERROR') {
2241 CHK("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2245 # Need a space before open parenthesis after if, while etc
2246 if ($line=~/\b(if|while|for|switch)\(/) {
2247 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2250 # Check for illegal assignment in if conditional -- and check for trailing
2251 # statements after the conditional.
2252 if ($line =~ /do\s*(?!{)/) {
2253 my ($stat_next) = ctx_statement_block($line_nr_next,
2254 $remain_next, $off_next);
2255 $stat_next =~ s/\n./\n /g;
2256 ##print "stat<$stat> stat_next<$stat_next>\n";
2258 if ($stat_next =~ /^\s*while\b/) {
2259 # If the statement carries leading newlines,
2260 # then count those as offsets.
2261 my ($whitespace) =
2262 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2263 my $offset =
2264 statement_rawlines($whitespace) - 1;
2266 $suppress_whiletrailers{$line_nr_next +
2267 $offset} = 1;
2270 if (!defined $suppress_whiletrailers{$linenr} &&
2271 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2272 my ($s, $c) = ($stat, $cond);
2274 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2275 ERROR("do not use assignment in if condition\n" . $herecurr);
2278 # Find out what is on the end of the line after the
2279 # conditional.
2280 substr($s, 0, length($c), '');
2281 $s =~ s/\n.*//g;
2282 $s =~ s/$;//g; # Remove any comments
2283 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2284 $c !~ /}\s*while\s*/)
2286 # Find out how long the conditional actually is.
2287 my @newlines = ($c =~ /\n/gs);
2288 my $cond_lines = 1 + $#newlines;
2289 my $stat_real = '';
2291 $stat_real = raw_line($linenr, $cond_lines)
2292 . "\n" if ($cond_lines);
2293 if (defined($stat_real) && $cond_lines > 1) {
2294 $stat_real = "[...]\n$stat_real";
2297 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2301 # Check for bitwise tests written as boolean
2302 if ($line =~ /
2304 (?:\[|\(|\&\&|\|\|)
2305 \s*0[xX][0-9]+\s*
2306 (?:\&\&|\|\|)
2308 (?:\&\&|\|\|)
2309 \s*0[xX][0-9]+\s*
2310 (?:\&\&|\|\||\)|\])
2311 )/x)
2313 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2316 # if and else should not have general statements after it
2317 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2318 my $s = $1;
2319 $s =~ s/$;//g; # Remove any comments
2320 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2321 ERROR("trailing statements should be on next line\n" . $herecurr);
2324 # if should not continue a brace
2325 if ($line =~ /}\s*if\b/) {
2326 ERROR("trailing statements should be on next line\n" .
2327 $herecurr);
2329 # case and default should not have general statements after them
2330 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2331 $line !~ /\G(?:
2332 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2333 \s*return\s+
2334 )/xg)
2336 ERROR("trailing statements should be on next line\n" . $herecurr);
2339 # Check for }<nl>else {, these must be at the same
2340 # indent level to be relevant to each other.
2341 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2342 $previndent == $indent) {
2343 ERROR("else should follow close brace '}'\n" . $hereprev);
2346 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2347 $previndent == $indent) {
2348 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2350 # Find out what is on the end of the line after the
2351 # conditional.
2352 substr($s, 0, length($c), '');
2353 $s =~ s/\n.*//g;
2355 if ($s =~ /^\s*;/) {
2356 ERROR("while should follow close brace '}'\n" . $hereprev);
2360 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2361 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2362 # print "No studly caps, use _\n";
2363 # print "$herecurr";
2364 # $clean = 0;
2367 #no spaces allowed after \ in define
2368 if ($line=~/\#\s*define.*\\\s$/) {
2369 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2372 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2373 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2374 my $file = "$1.h";
2375 my $checkfile = "include/linux/$file";
2376 if (-f "$root/$checkfile" &&
2377 $realfile ne $checkfile &&
2378 $1 !~ /$allowed_asm_includes/)
2380 if ($realfile =~ m{^arch/}) {
2381 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2382 } else {
2383 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2388 # multi-statement macros should be enclosed in a do while loop, grab the
2389 # first statement and ensure its the whole macro if its not enclosed
2390 # in a known good container
2391 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2392 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2393 my $ln = $linenr;
2394 my $cnt = $realcnt;
2395 my ($off, $dstat, $dcond, $rest);
2396 my $ctx = '';
2398 my $args = defined($1);
2400 # Find the end of the macro and limit our statement
2401 # search to that.
2402 while ($cnt > 0 && defined $lines[$ln - 1] &&
2403 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2405 $ctx .= $rawlines[$ln - 1] . "\n";
2406 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2407 $ln++;
2409 $ctx .= $rawlines[$ln - 1];
2411 ($dstat, $dcond, $ln, $cnt, $off) =
2412 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2413 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2414 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2416 # Extract the remainder of the define (if any) and
2417 # rip off surrounding spaces, and trailing \'s.
2418 $rest = '';
2419 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2420 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2421 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2422 $rest .= substr($lines[$ln - 1], $off) . "\n";
2423 $cnt--;
2425 $ln++;
2426 $off = 0;
2428 $rest =~ s/\\\n.//g;
2429 $rest =~ s/^\s*//s;
2430 $rest =~ s/\s*$//s;
2432 # Clean up the original statement.
2433 if ($args) {
2434 substr($dstat, 0, length($dcond), '');
2435 } else {
2436 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2438 $dstat =~ s/$;//g;
2439 $dstat =~ s/\\\n.//g;
2440 $dstat =~ s/^\s*//s;
2441 $dstat =~ s/\s*$//s;
2443 # Flatten any parentheses and braces
2444 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2445 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2446 $dstat =~ s/\[[^\{\}]*\]/1/)
2450 my $exceptions = qr{
2451 $Declare|
2452 module_param_named|
2453 MODULE_PARAM_DESC|
2454 DECLARE_PER_CPU|
2455 DEFINE_PER_CPU|
2456 __typeof__\(|
2457 union|
2458 struct|
2459 \.$Ident\s*=\s*|
2460 ^\"|\"$
2462 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2463 if ($rest ne '' && $rest ne ',') {
2464 if ($rest !~ /while\s*\(/ &&
2465 $dstat !~ /$exceptions/)
2467 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2470 } elsif ($ctx !~ /;/) {
2471 if ($dstat ne '' &&
2472 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2473 $dstat !~ /$exceptions/ &&
2474 $dstat !~ /^\.$Ident\s*=/ &&
2475 $dstat =~ /$Operators/)
2477 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2482 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2483 # all assignments may have only one of the following with an assignment:
2485 # ALIGN(...)
2486 # VMLINUX_SYMBOL(...)
2487 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2488 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2491 # check for missing bracing round if etc
2492 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2493 my ($level, $endln, @chunks) =
2494 ctx_statement_full($linenr, $realcnt, 1);
2495 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2496 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2497 if ($#chunks >= 0 && $level == 0) {
2498 my $allowed = 0;
2499 my $seen = 0;
2500 my $herectx = $here . "\n";
2501 my $ln = $linenr - 1;
2502 for my $chunk (@chunks) {
2503 my ($cond, $block) = @{$chunk};
2505 # If the condition carries leading newlines, then count those as offsets.
2506 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2507 my $offset = statement_rawlines($whitespace) - 1;
2509 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2511 # We have looked at and allowed this specific line.
2512 $suppress_ifbraces{$ln + $offset} = 1;
2514 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2515 $ln += statement_rawlines($block) - 1;
2517 substr($block, 0, length($cond), '');
2519 $seen++ if ($block =~ /^\s*{/);
2521 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2522 if (statement_lines($cond) > 1) {
2523 #print "APW: ALLOWED: cond<$cond>\n";
2524 $allowed = 1;
2526 if ($block =~/\b(?:if|for|while)\b/) {
2527 #print "APW: ALLOWED: block<$block>\n";
2528 $allowed = 1;
2530 if (statement_block_size($block) > 1) {
2531 #print "APW: ALLOWED: lines block<$block>\n";
2532 $allowed = 1;
2535 if ($seen != ($#chunks + 1)) {
2536 WARN("braces {} are necessary for all arms of this statement\n" . $herectx);
2540 if (!defined $suppress_ifbraces{$linenr - 1} &&
2541 $line =~ /\b(if|while|for|else)\b/ &&
2542 $line !~ /\#\s*if/ &&
2543 $line !~ /\#\s*else/) {
2544 my $allowed = 0;
2546 # Check the pre-context.
2547 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2548 #print "APW: ALLOWED: pre<$1>\n";
2549 $allowed = 1;
2552 my ($level, $endln, @chunks) =
2553 ctx_statement_full($linenr, $realcnt, $-[0]);
2555 # Check the condition.
2556 my ($cond, $block) = @{$chunks[0]};
2557 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2558 if (defined $cond) {
2559 substr($block, 0, length($cond), '');
2561 if (statement_lines($cond) > 1) {
2562 #print "APW: ALLOWED: cond<$cond>\n";
2563 $allowed = 1;
2565 if ($block =~/\b(?:if|for|while)\b/) {
2566 #print "APW: ALLOWED: block<$block>\n";
2567 $allowed = 1;
2569 if (statement_block_size($block) > 1) {
2570 #print "APW: ALLOWED: lines block<$block>\n";
2571 $allowed = 1;
2573 # Check the post-context.
2574 if (defined $chunks[1]) {
2575 my ($cond, $block) = @{$chunks[1]};
2576 if (defined $cond) {
2577 substr($block, 0, length($cond), '');
2579 if ($block =~ /^\s*\{/) {
2580 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2581 $allowed = 1;
2584 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2585 my $herectx = $here . "\n";;
2586 my $cnt = statement_rawlines($block);
2588 for (my $n = 0; $n < $cnt; $n++) {
2589 $herectx .= raw_line($linenr, $n) . "\n";;
2592 WARN("braces {} are necessary even for single statement blocks\n" . $herectx);
2596 # don't include deprecated include files (uses RAW line)
2597 for my $inc (@dep_includes) {
2598 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2599 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2603 # don't use deprecated functions
2604 for my $func (@dep_functions) {
2605 if ($line =~ /\b$func\b/) {
2606 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2610 # no volatiles please
2611 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2612 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2613 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2616 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2617 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2618 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2621 # warn about #if 0
2622 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2623 CHK("if this code is redundant consider removing it\n" .
2624 $herecurr);
2627 # check for needless kfree() checks
2628 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2629 my $expr = $1;
2630 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2631 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2634 # check for needless usb_free_urb() checks
2635 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2636 my $expr = $1;
2637 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2638 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2642 # prefer usleep_range over udelay
2643 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2644 # ignore udelay's < 10, however
2645 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2646 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2650 # warn about unexpectedly long msleep's
2651 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2652 if ($1 < 20) {
2653 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2657 # warn about #ifdefs in C files
2658 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2659 # print "#ifdef in C files should be avoided\n";
2660 # print "$herecurr";
2661 # $clean = 0;
2664 # warn about spacing in #ifdefs
2665 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2666 ERROR("exactly one space required after that #$1\n" . $herecurr);
2669 # check for spinlock_t definitions without a comment.
2670 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2671 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2672 my $which = $1;
2673 if (!ctx_has_comment($first_line, $linenr)) {
2674 CHK("$1 definition without comment\n" . $herecurr);
2677 # check for memory barriers without a comment.
2678 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2679 if (!ctx_has_comment($first_line, $linenr)) {
2680 CHK("memory barrier without comment\n" . $herecurr);
2683 # check of hardware specific defines
2684 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2685 CHK("architecture specific defines should be avoided\n" . $herecurr);
2688 # Check that the storage class is at the beginning of a declaration
2689 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2690 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2693 # check the location of the inline attribute, that it is between
2694 # storage class and type.
2695 if ($line =~ /\b$Type\s+$Inline\b/ ||
2696 $line =~ /\b$Inline\s+$Storage\b/) {
2697 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2700 # Check for __inline__ and __inline, prefer inline
2701 if ($line =~ /\b(__inline__|__inline)\b/) {
2702 WARN("plain inline is preferred over $1\n" . $herecurr);
2705 # check for sizeof(&)
2706 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2707 WARN("sizeof(& should be avoided\n" . $herecurr);
2710 # check for new externs in .c files.
2711 if ($realfile =~ /\.c$/ && defined $stat &&
2712 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2714 my $function_name = $1;
2715 my $paren_space = $2;
2717 my $s = $stat;
2718 if (defined $cond) {
2719 substr($s, 0, length($cond), '');
2721 if ($s =~ /^\s*;/ &&
2722 $function_name ne 'uninitialized_var')
2724 WARN("externs should be avoided in .c files\n" . $herecurr);
2727 if ($paren_space =~ /\n/) {
2728 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2731 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2732 $stat =~ /^.\s*extern\s+/)
2734 WARN("externs should be avoided in .c files\n" . $herecurr);
2737 # checks for new __setup's
2738 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2739 my $name = $1;
2741 if (!grep(/$name/, @setup_docs)) {
2742 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2746 # check for pointless casting of kmalloc return
2747 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2748 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2751 # check for gcc specific __FUNCTION__
2752 if ($line =~ /__FUNCTION__/) {
2753 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2756 # check for semaphores used as mutexes
2757 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2758 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2760 # check for semaphores used as mutexes
2761 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2762 WARN("consider using a completion\n" . $herecurr);
2765 # recommend strict_strto* over simple_strto*
2766 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2767 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2769 # check for __initcall(), use device_initcall() explicitly please
2770 if ($line =~ /^.\s*__initcall\s*\(/) {
2771 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2773 # check for various ops structs, ensure they are const.
2774 my $struct_ops = qr{acpi_dock_ops|
2775 address_space_operations|
2776 backlight_ops|
2777 block_device_operations|
2778 dentry_operations|
2779 dev_pm_ops|
2780 dma_map_ops|
2781 extent_io_ops|
2782 file_lock_operations|
2783 file_operations|
2784 hv_ops|
2785 ide_dma_ops|
2786 intel_dvo_dev_ops|
2787 item_operations|
2788 iwl_ops|
2789 kgdb_arch|
2790 kgdb_io|
2791 kset_uevent_ops|
2792 lock_manager_operations|
2793 microcode_ops|
2794 mtrr_ops|
2795 neigh_ops|
2796 nlmsvc_binding|
2797 pci_raw_ops|
2798 pipe_buf_operations|
2799 platform_hibernation_ops|
2800 platform_suspend_ops|
2801 proto_ops|
2802 rpc_pipe_ops|
2803 seq_operations|
2804 snd_ac97_build_ops|
2805 soc_pcmcia_socket_ops|
2806 stacktrace_ops|
2807 sysfs_ops|
2808 tty_operations|
2809 usb_mon_operations|
2810 wd_ops}x;
2811 if ($line !~ /\bconst\b/ &&
2812 $line =~ /\bstruct\s+($struct_ops)\b/) {
2813 WARN("struct $1 should normally be const\n" .
2814 $herecurr);
2817 # use of NR_CPUS is usually wrong
2818 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2819 if ($line =~ /\bNR_CPUS\b/ &&
2820 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2821 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2822 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2823 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2824 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2826 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2829 # check for %L{u,d,i} in strings
2830 my $string;
2831 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2832 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2833 $string =~ s/%%/__/g;
2834 if ($string =~ /(?<!%)%L[udi]/) {
2835 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2836 last;
2840 # whine mightly about in_atomic
2841 if ($line =~ /\bin_atomic\s*\(/) {
2842 if ($realfile =~ m@^drivers/@) {
2843 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2844 } elsif ($realfile !~ m@^kernel/@) {
2845 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2849 # check for lockdep_set_novalidate_class
2850 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2851 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2852 if ($realfile !~ m@^kernel/lockdep@ &&
2853 $realfile !~ m@^include/linux/lockdep@ &&
2854 $realfile !~ m@^drivers/base/core@) {
2855 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2860 # If we have no input at all, then there is nothing to report on
2861 # so just keep quiet.
2862 if ($#rawlines == -1) {
2863 exit(0);
2866 # In mailback mode only produce a report in the negative, for
2867 # things that appear to be patches.
2868 if ($mailback && ($clean == 1 || !$is_patch)) {
2869 exit(0);
2872 # This is not a patch, and we are are in 'no-patch' mode so
2873 # just keep quiet.
2874 if (!$chk_patch && !$is_patch) {
2875 exit(0);
2878 if (!$is_patch) {
2879 ERROR("Does not appear to be a unified-diff format patch\n");
2881 if ($is_patch && $chk_signoff && $signoff == 0) {
2882 ERROR("Missing Signed-off-by: line(s)\n");
2885 print report_dump();
2886 if ($summary && !($clean == 1 && $quiet == 1)) {
2887 print "$filename " if ($summary_file);
2888 print "total: $cnt_error errors, $cnt_warn warnings, " .
2889 (($check)? "$cnt_chk checks, " : "") .
2890 "$cnt_lines lines checked\n";
2891 print "\n" if ($quiet == 0);
2894 if ($quiet == 0) {
2895 # If there were whitespace errors which cleanpatch can fix
2896 # then suggest that.
2897 # if ($rpt_cleaners) {
2898 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2899 # print " scripts/cleanfile\n\n";
2903 if ($clean == 1 && $quiet == 0) {
2904 print "$vname has no obvious style problems and is ready for submission.\n"
2906 if ($clean == 0 && $quiet == 0) {
2907 print "$vname has style problems, please review. If any of these errors\n";
2908 print "are false positives report them to the maintainer, see\n";
2909 print "CHECKPATCH in MAINTAINERS.\n";
2912 return $clean;