checkpatch: fix handling of leading spaces
[deliverable/linux.git] / scripts / checkpatch.pl
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
7
8 use strict;
9
10 my $P = $0;
11 $P =~ s@.*/@@g;
12
13 my $V = '0.30';
14
15 use Getopt::Long qw(:config no_auto_abbrev);
16
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;
32
33 sub help {
34 my ($exitcode) = @_;
35
36 print << "EOM";
37 Usage: $P [OPTION]... [FILE]...
38 Version: $V
39
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
59
60 When FILE is - read standard input.
61 EOM
62
63 exit($exitcode);
64 }
65
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,
80
81 'debug=s' => \%debug,
82 'test-only=s' => \$tst_only,
83 'h|help' => \$help,
84 'version' => \$help
85 ) or help(1);
86
87 help(0) if ($help);
88
89 my $exit = 0;
90
91 if ($#ARGV < 0) {
92 print "$P: no input files\n";
93 exit(1);
94 }
95
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 ($@);
104 }
105
106 if ($terse) {
107 $emacs = 1;
108 $quiet++;
109 }
110
111 if ($tree) {
112 if (defined $root) {
113 if (!top_of_kernel_tree($root)) {
114 die "$P: $root: --root does not point at a valid tree\n";
115 }
116 } else {
117 if (top_of_kernel_tree('.')) {
118 $root = '.';
119 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
120 top_of_kernel_tree($1)) {
121 $root = $1;
122 }
123 }
124
125 if (!defined $root) {
126 print "Must be run from the top-level dir. of a kernel tree\n";
127 exit(2);
128 }
129 }
130
131 my $emitted_corrupt = 0;
132
133 our $Ident = qr{
134 [A-Za-z_][A-Za-z\d_]*
135 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
136 }x;
137 our $Storage = qr{extern|static|asmlinkage};
138 our $Sparse = qr{
139 __user|
140 __kernel|
141 __force|
142 __iomem|
143 __must_check|
144 __init_refok|
145 __kprobes|
146 __ref
147 }x;
148
149 # Notes to $Attribute:
150 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
151 our $Attribute = qr{
152 const|
153 __read_mostly|
154 __kprobes|
155 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
156 ____cacheline_aligned|
157 ____cacheline_aligned_in_smp|
158 ____cacheline_internodealigned_in_smp|
159 __weak
160 }x;
161 our $Modifier;
162 our $Inline = qr{inline|__always_inline|noinline};
163 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
164 our $Lval = qr{$Ident(?:$Member)*};
165
166 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
167 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
168 our $Compare = qr{<=|>=|==|!=|<|>};
169 our $Operators = qr{
170 <=|>=|==|!=|
171 =>|->|<<|>>|<|>|!|~|
172 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
173 }x;
174
175 our $NonptrType;
176 our $Type;
177 our $Declare;
178
179 our $UTF8 = qr {
180 [\x09\x0A\x0D\x20-\x7E] # ASCII
181 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
182 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
183 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
184 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
185 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
186 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
187 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
188 }x;
189
190 our $typeTypedefs = qr{(?x:
191 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
192 atomic_t
193 )};
194
195 our $logFunctions = qr{(?x:
196 printk|
197 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
198 dev_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
199 WARN|
200 panic
201 )};
202
203 our @typeList = (
204 qr{void},
205 qr{(?:unsigned\s+)?char},
206 qr{(?:unsigned\s+)?short},
207 qr{(?:unsigned\s+)?int},
208 qr{(?:unsigned\s+)?long},
209 qr{(?:unsigned\s+)?long\s+int},
210 qr{(?:unsigned\s+)?long\s+long},
211 qr{(?:unsigned\s+)?long\s+long\s+int},
212 qr{unsigned},
213 qr{float},
214 qr{double},
215 qr{bool},
216 qr{struct\s+$Ident},
217 qr{union\s+$Ident},
218 qr{enum\s+$Ident},
219 qr{${Ident}_t},
220 qr{${Ident}_handler},
221 qr{${Ident}_handler_fn},
222 );
223 our @modifierList = (
224 qr{fastcall},
225 );
226
227 our $allowed_asm_includes = qr{(?x:
228 irq|
229 memory
230 )};
231 # memory.h: ARM has a custom one
232
233 sub build_types {
234 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
235 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
236 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
237 $NonptrType = qr{
238 (?:$Modifier\s+|const\s+)*
239 (?:
240 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
241 (?:$typeTypedefs\b)|
242 (?:${all}\b)
243 )
244 (?:\s+$Modifier|\s+const)*
245 }x;
246 $Type = qr{
247 $NonptrType
248 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
249 (?:\s+$Inline|\s+$Modifier)*
250 }x;
251 $Declare = qr{(?:$Storage\s+)?$Type};
252 }
253 build_types();
254
255 $chk_signoff = 0 if ($file);
256
257 my @dep_includes = ();
258 my @dep_functions = ();
259 my $removal = "Documentation/feature-removal-schedule.txt";
260 if ($tree && -f "$root/$removal") {
261 open(my $REMOVE, '<', "$root/$removal") ||
262 die "$P: $removal: open failed - $!\n";
263 while (<$REMOVE>) {
264 if (/^Check:\s+(.*\S)/) {
265 for my $entry (split(/[, ]+/, $1)) {
266 if ($entry =~ m@include/(.*)@) {
267 push(@dep_includes, $1);
268
269 } elsif ($entry !~ m@/@) {
270 push(@dep_functions, $entry);
271 }
272 }
273 }
274 }
275 close($REMOVE);
276 }
277
278 my @rawlines = ();
279 my @lines = ();
280 my $vname;
281 for my $filename (@ARGV) {
282 my $FILE;
283 if ($file) {
284 open($FILE, '-|', "diff -u /dev/null $filename") ||
285 die "$P: $filename: diff failed - $!\n";
286 } elsif ($filename eq '-') {
287 open($FILE, '<&STDIN');
288 } else {
289 open($FILE, '<', "$filename") ||
290 die "$P: $filename: open failed - $!\n";
291 }
292 if ($filename eq '-') {
293 $vname = 'Your patch';
294 } else {
295 $vname = $filename;
296 }
297 while (<$FILE>) {
298 chomp;
299 push(@rawlines, $_);
300 }
301 close($FILE);
302 if (!process($filename)) {
303 $exit = 1;
304 }
305 @rawlines = ();
306 @lines = ();
307 }
308
309 exit($exit);
310
311 sub top_of_kernel_tree {
312 my ($root) = @_;
313
314 my @tree_check = (
315 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
316 "README", "Documentation", "arch", "include", "drivers",
317 "fs", "init", "ipc", "kernel", "lib", "scripts",
318 );
319
320 foreach my $check (@tree_check) {
321 if (! -e $root . '/' . $check) {
322 return 0;
323 }
324 }
325 return 1;
326 }
327
328 sub expand_tabs {
329 my ($str) = @_;
330
331 my $res = '';
332 my $n = 0;
333 for my $c (split(//, $str)) {
334 if ($c eq "\t") {
335 $res .= ' ';
336 $n++;
337 for (; ($n % 8) != 0; $n++) {
338 $res .= ' ';
339 }
340 next;
341 }
342 $res .= $c;
343 $n++;
344 }
345
346 return $res;
347 }
348 sub copy_spacing {
349 (my $res = shift) =~ tr/\t/ /c;
350 return $res;
351 }
352
353 sub line_stats {
354 my ($line) = @_;
355
356 # Drop the diff line leader and expand tabs
357 $line =~ s/^.//;
358 $line = expand_tabs($line);
359
360 # Pick the indent from the front of the line.
361 my ($white) = ($line =~ /^(\s*)/);
362
363 return (length($line), length($white));
364 }
365
366 my $sanitise_quote = '';
367
368 sub sanitise_line_reset {
369 my ($in_comment) = @_;
370
371 if ($in_comment) {
372 $sanitise_quote = '*/';
373 } else {
374 $sanitise_quote = '';
375 }
376 }
377 sub sanitise_line {
378 my ($line) = @_;
379
380 my $res = '';
381 my $l = '';
382
383 my $qlen = 0;
384 my $off = 0;
385 my $c;
386
387 # Always copy over the diff marker.
388 $res = substr($line, 0, 1);
389
390 for ($off = 1; $off < length($line); $off++) {
391 $c = substr($line, $off, 1);
392
393 # Comments we are wacking completly including the begin
394 # and end, all to $;.
395 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
396 $sanitise_quote = '*/';
397
398 substr($res, $off, 2, "$;$;");
399 $off++;
400 next;
401 }
402 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
403 $sanitise_quote = '';
404 substr($res, $off, 2, "$;$;");
405 $off++;
406 next;
407 }
408 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
409 $sanitise_quote = '//';
410
411 substr($res, $off, 2, $sanitise_quote);
412 $off++;
413 next;
414 }
415
416 # A \ in a string means ignore the next character.
417 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
418 $c eq "\\") {
419 substr($res, $off, 2, 'XX');
420 $off++;
421 next;
422 }
423 # Regular quotes.
424 if ($c eq "'" || $c eq '"') {
425 if ($sanitise_quote eq '') {
426 $sanitise_quote = $c;
427
428 substr($res, $off, 1, $c);
429 next;
430 } elsif ($sanitise_quote eq $c) {
431 $sanitise_quote = '';
432 }
433 }
434
435 #print "c<$c> SQ<$sanitise_quote>\n";
436 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
437 substr($res, $off, 1, $;);
438 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
439 substr($res, $off, 1, $;);
440 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
441 substr($res, $off, 1, 'X');
442 } else {
443 substr($res, $off, 1, $c);
444 }
445 }
446
447 if ($sanitise_quote eq '//') {
448 $sanitise_quote = '';
449 }
450
451 # The pathname on a #include may be surrounded by '<' and '>'.
452 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
453 my $clean = 'X' x length($1);
454 $res =~ s@\<.*\>@<$clean>@;
455
456 # The whole of a #error is a string.
457 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
458 my $clean = 'X' x length($1);
459 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
460 }
461
462 return $res;
463 }
464
465 sub ctx_statement_block {
466 my ($linenr, $remain, $off) = @_;
467 my $line = $linenr - 1;
468 my $blk = '';
469 my $soff = $off;
470 my $coff = $off - 1;
471 my $coff_set = 0;
472
473 my $loff = 0;
474
475 my $type = '';
476 my $level = 0;
477 my @stack = ();
478 my $p;
479 my $c;
480 my $len = 0;
481
482 my $remainder;
483 while (1) {
484 @stack = (['', 0]) if ($#stack == -1);
485
486 #warn "CSB: blk<$blk> remain<$remain>\n";
487 # If we are about to drop off the end, pull in more
488 # context.
489 if ($off >= $len) {
490 for (; $remain > 0; $line++) {
491 last if (!defined $lines[$line]);
492 next if ($lines[$line] =~ /^-/);
493 $remain--;
494 $loff = $len;
495 $blk .= $lines[$line] . "\n";
496 $len = length($blk);
497 $line++;
498 last;
499 }
500 # Bail if there is no further context.
501 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
502 if ($off >= $len) {
503 last;
504 }
505 }
506 $p = $c;
507 $c = substr($blk, $off, 1);
508 $remainder = substr($blk, $off);
509
510 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
511
512 # Handle nested #if/#else.
513 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
514 push(@stack, [ $type, $level ]);
515 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
516 ($type, $level) = @{$stack[$#stack - 1]};
517 } elsif ($remainder =~ /^#\s*endif\b/) {
518 ($type, $level) = @{pop(@stack)};
519 }
520
521 # Statement ends at the ';' or a close '}' at the
522 # outermost level.
523 if ($level == 0 && $c eq ';') {
524 last;
525 }
526
527 # An else is really a conditional as long as its not else if
528 if ($level == 0 && $coff_set == 0 &&
529 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
530 $remainder =~ /^(else)(?:\s|{)/ &&
531 $remainder !~ /^else\s+if\b/) {
532 $coff = $off + length($1) - 1;
533 $coff_set = 1;
534 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
535 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
536 }
537
538 if (($type eq '' || $type eq '(') && $c eq '(') {
539 $level++;
540 $type = '(';
541 }
542 if ($type eq '(' && $c eq ')') {
543 $level--;
544 $type = ($level != 0)? '(' : '';
545
546 if ($level == 0 && $coff < $soff) {
547 $coff = $off;
548 $coff_set = 1;
549 #warn "CSB: mark coff<$coff>\n";
550 }
551 }
552 if (($type eq '' || $type eq '{') && $c eq '{') {
553 $level++;
554 $type = '{';
555 }
556 if ($type eq '{' && $c eq '}') {
557 $level--;
558 $type = ($level != 0)? '{' : '';
559
560 if ($level == 0) {
561 last;
562 }
563 }
564 $off++;
565 }
566 # We are truly at the end, so shuffle to the next line.
567 if ($off == $len) {
568 $loff = $len + 1;
569 $line++;
570 $remain--;
571 }
572
573 my $statement = substr($blk, $soff, $off - $soff + 1);
574 my $condition = substr($blk, $soff, $coff - $soff + 1);
575
576 #warn "STATEMENT<$statement>\n";
577 #warn "CONDITION<$condition>\n";
578
579 #print "coff<$coff> soff<$off> loff<$loff>\n";
580
581 return ($statement, $condition,
582 $line, $remain + 1, $off - $loff + 1, $level);
583 }
584
585 sub statement_lines {
586 my ($stmt) = @_;
587
588 # Strip the diff line prefixes and rip blank lines at start and end.
589 $stmt =~ s/(^|\n)./$1/g;
590 $stmt =~ s/^\s*//;
591 $stmt =~ s/\s*$//;
592
593 my @stmt_lines = ($stmt =~ /\n/g);
594
595 return $#stmt_lines + 2;
596 }
597
598 sub statement_rawlines {
599 my ($stmt) = @_;
600
601 my @stmt_lines = ($stmt =~ /\n/g);
602
603 return $#stmt_lines + 2;
604 }
605
606 sub statement_block_size {
607 my ($stmt) = @_;
608
609 $stmt =~ s/(^|\n)./$1/g;
610 $stmt =~ s/^\s*{//;
611 $stmt =~ s/}\s*$//;
612 $stmt =~ s/^\s*//;
613 $stmt =~ s/\s*$//;
614
615 my @stmt_lines = ($stmt =~ /\n/g);
616 my @stmt_statements = ($stmt =~ /;/g);
617
618 my $stmt_lines = $#stmt_lines + 2;
619 my $stmt_statements = $#stmt_statements + 1;
620
621 if ($stmt_lines > $stmt_statements) {
622 return $stmt_lines;
623 } else {
624 return $stmt_statements;
625 }
626 }
627
628 sub ctx_statement_full {
629 my ($linenr, $remain, $off) = @_;
630 my ($statement, $condition, $level);
631
632 my (@chunks);
633
634 # Grab the first conditional/block pair.
635 ($statement, $condition, $linenr, $remain, $off, $level) =
636 ctx_statement_block($linenr, $remain, $off);
637 #print "F: c<$condition> s<$statement> remain<$remain>\n";
638 push(@chunks, [ $condition, $statement ]);
639 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
640 return ($level, $linenr, @chunks);
641 }
642
643 # Pull in the following conditional/block pairs and see if they
644 # could continue the statement.
645 for (;;) {
646 ($statement, $condition, $linenr, $remain, $off, $level) =
647 ctx_statement_block($linenr, $remain, $off);
648 #print "C: c<$condition> s<$statement> remain<$remain>\n";
649 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
650 #print "C: push\n";
651 push(@chunks, [ $condition, $statement ]);
652 }
653
654 return ($level, $linenr, @chunks);
655 }
656
657 sub ctx_block_get {
658 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
659 my $line;
660 my $start = $linenr - 1;
661 my $blk = '';
662 my @o;
663 my @c;
664 my @res = ();
665
666 my $level = 0;
667 my @stack = ($level);
668 for ($line = $start; $remain > 0; $line++) {
669 next if ($rawlines[$line] =~ /^-/);
670 $remain--;
671
672 $blk .= $rawlines[$line];
673
674 # Handle nested #if/#else.
675 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
676 push(@stack, $level);
677 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
678 $level = $stack[$#stack - 1];
679 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
680 $level = pop(@stack);
681 }
682
683 foreach my $c (split(//, $rawlines[$line])) {
684 ##print "C<$c>L<$level><$open$close>O<$off>\n";
685 if ($off > 0) {
686 $off--;
687 next;
688 }
689
690 if ($c eq $close && $level > 0) {
691 $level--;
692 last if ($level == 0);
693 } elsif ($c eq $open) {
694 $level++;
695 }
696 }
697
698 if (!$outer || $level <= 1) {
699 push(@res, $rawlines[$line]);
700 }
701
702 last if ($level == 0);
703 }
704
705 return ($level, @res);
706 }
707 sub ctx_block_outer {
708 my ($linenr, $remain) = @_;
709
710 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
711 return @r;
712 }
713 sub ctx_block {
714 my ($linenr, $remain) = @_;
715
716 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
717 return @r;
718 }
719 sub ctx_statement {
720 my ($linenr, $remain, $off) = @_;
721
722 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
723 return @r;
724 }
725 sub ctx_block_level {
726 my ($linenr, $remain) = @_;
727
728 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
729 }
730 sub ctx_statement_level {
731 my ($linenr, $remain, $off) = @_;
732
733 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
734 }
735
736 sub ctx_locate_comment {
737 my ($first_line, $end_line) = @_;
738
739 # Catch a comment on the end of the line itself.
740 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
741 return $current_comment if (defined $current_comment);
742
743 # Look through the context and try and figure out if there is a
744 # comment.
745 my $in_comment = 0;
746 $current_comment = '';
747 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
748 my $line = $rawlines[$linenr - 1];
749 #warn " $line\n";
750 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
751 $in_comment = 1;
752 }
753 if ($line =~ m@/\*@) {
754 $in_comment = 1;
755 }
756 if (!$in_comment && $current_comment ne '') {
757 $current_comment = '';
758 }
759 $current_comment .= $line . "\n" if ($in_comment);
760 if ($line =~ m@\*/@) {
761 $in_comment = 0;
762 }
763 }
764
765 chomp($current_comment);
766 return($current_comment);
767 }
768 sub ctx_has_comment {
769 my ($first_line, $end_line) = @_;
770 my $cmt = ctx_locate_comment($first_line, $end_line);
771
772 ##print "LINE: $rawlines[$end_line - 1 ]\n";
773 ##print "CMMT: $cmt\n";
774
775 return ($cmt ne '');
776 }
777
778 sub raw_line {
779 my ($linenr, $cnt) = @_;
780
781 my $offset = $linenr - 1;
782 $cnt++;
783
784 my $line;
785 while ($cnt) {
786 $line = $rawlines[$offset++];
787 next if (defined($line) && $line =~ /^-/);
788 $cnt--;
789 }
790
791 return $line;
792 }
793
794 sub cat_vet {
795 my ($vet) = @_;
796 my ($res, $coded);
797
798 $res = '';
799 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
800 $res .= $1;
801 if ($2 ne '') {
802 $coded = sprintf("^%c", unpack('C', $2) + 64);
803 $res .= $coded;
804 }
805 }
806 $res =~ s/$/\$/;
807
808 return $res;
809 }
810
811 my $av_preprocessor = 0;
812 my $av_pending;
813 my @av_paren_type;
814 my $av_pend_colon;
815
816 sub annotate_reset {
817 $av_preprocessor = 0;
818 $av_pending = '_';
819 @av_paren_type = ('E');
820 $av_pend_colon = 'O';
821 }
822
823 sub annotate_values {
824 my ($stream, $type) = @_;
825
826 my $res;
827 my $var = '_' x length($stream);
828 my $cur = $stream;
829
830 print "$stream\n" if ($dbg_values > 1);
831
832 while (length($cur)) {
833 @av_paren_type = ('E') if ($#av_paren_type < 0);
834 print " <" . join('', @av_paren_type) .
835 "> <$type> <$av_pending>" if ($dbg_values > 1);
836 if ($cur =~ /^(\s+)/o) {
837 print "WS($1)\n" if ($dbg_values > 1);
838 if ($1 =~ /\n/ && $av_preprocessor) {
839 $type = pop(@av_paren_type);
840 $av_preprocessor = 0;
841 }
842
843 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
844 print "DECLARE($1)\n" if ($dbg_values > 1);
845 $type = 'T';
846
847 } elsif ($cur =~ /^($Modifier)\s*/) {
848 print "MODIFIER($1)\n" if ($dbg_values > 1);
849 $type = 'T';
850
851 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
852 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
853 $av_preprocessor = 1;
854 push(@av_paren_type, $type);
855 if ($2 ne '') {
856 $av_pending = 'N';
857 }
858 $type = 'E';
859
860 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
861 print "UNDEF($1)\n" if ($dbg_values > 1);
862 $av_preprocessor = 1;
863 push(@av_paren_type, $type);
864
865 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
866 print "PRE_START($1)\n" if ($dbg_values > 1);
867 $av_preprocessor = 1;
868
869 push(@av_paren_type, $type);
870 push(@av_paren_type, $type);
871 $type = 'E';
872
873 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
874 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
875 $av_preprocessor = 1;
876
877 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
878
879 $type = 'E';
880
881 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
882 print "PRE_END($1)\n" if ($dbg_values > 1);
883
884 $av_preprocessor = 1;
885
886 # Assume all arms of the conditional end as this
887 # one does, and continue as if the #endif was not here.
888 pop(@av_paren_type);
889 push(@av_paren_type, $type);
890 $type = 'E';
891
892 } elsif ($cur =~ /^(\\\n)/o) {
893 print "PRECONT($1)\n" if ($dbg_values > 1);
894
895 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
896 print "ATTR($1)\n" if ($dbg_values > 1);
897 $av_pending = $type;
898 $type = 'N';
899
900 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
901 print "SIZEOF($1)\n" if ($dbg_values > 1);
902 if (defined $2) {
903 $av_pending = 'V';
904 }
905 $type = 'N';
906
907 } elsif ($cur =~ /^(if|while|for)\b/o) {
908 print "COND($1)\n" if ($dbg_values > 1);
909 $av_pending = 'E';
910 $type = 'N';
911
912 } elsif ($cur =~/^(case)/o) {
913 print "CASE($1)\n" if ($dbg_values > 1);
914 $av_pend_colon = 'C';
915 $type = 'N';
916
917 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
918 print "KEYWORD($1)\n" if ($dbg_values > 1);
919 $type = 'N';
920
921 } elsif ($cur =~ /^(\()/o) {
922 print "PAREN('$1')\n" if ($dbg_values > 1);
923 push(@av_paren_type, $av_pending);
924 $av_pending = '_';
925 $type = 'N';
926
927 } elsif ($cur =~ /^(\))/o) {
928 my $new_type = pop(@av_paren_type);
929 if ($new_type ne '_') {
930 $type = $new_type;
931 print "PAREN('$1') -> $type\n"
932 if ($dbg_values > 1);
933 } else {
934 print "PAREN('$1')\n" if ($dbg_values > 1);
935 }
936
937 } elsif ($cur =~ /^($Ident)\s*\(/o) {
938 print "FUNC($1)\n" if ($dbg_values > 1);
939 $type = 'V';
940 $av_pending = 'V';
941
942 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
943 if (defined $2 && $type eq 'C' || $type eq 'T') {
944 $av_pend_colon = 'B';
945 } elsif ($type eq 'E') {
946 $av_pend_colon = 'L';
947 }
948 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
949 $type = 'V';
950
951 } elsif ($cur =~ /^($Ident|$Constant)/o) {
952 print "IDENT($1)\n" if ($dbg_values > 1);
953 $type = 'V';
954
955 } elsif ($cur =~ /^($Assignment)/o) {
956 print "ASSIGN($1)\n" if ($dbg_values > 1);
957 $type = 'N';
958
959 } elsif ($cur =~/^(;|{|})/) {
960 print "END($1)\n" if ($dbg_values > 1);
961 $type = 'E';
962 $av_pend_colon = 'O';
963
964 } elsif ($cur =~/^(,)/) {
965 print "COMMA($1)\n" if ($dbg_values > 1);
966 $type = 'C';
967
968 } elsif ($cur =~ /^(\?)/o) {
969 print "QUESTION($1)\n" if ($dbg_values > 1);
970 $type = 'N';
971
972 } elsif ($cur =~ /^(:)/o) {
973 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
974
975 substr($var, length($res), 1, $av_pend_colon);
976 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
977 $type = 'E';
978 } else {
979 $type = 'N';
980 }
981 $av_pend_colon = 'O';
982
983 } elsif ($cur =~ /^(\[)/o) {
984 print "CLOSE($1)\n" if ($dbg_values > 1);
985 $type = 'N';
986
987 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
988 my $variant;
989
990 print "OPV($1)\n" if ($dbg_values > 1);
991 if ($type eq 'V') {
992 $variant = 'B';
993 } else {
994 $variant = 'U';
995 }
996
997 substr($var, length($res), 1, $variant);
998 $type = 'N';
999
1000 } elsif ($cur =~ /^($Operators)/o) {
1001 print "OP($1)\n" if ($dbg_values > 1);
1002 if ($1 ne '++' && $1 ne '--') {
1003 $type = 'N';
1004 }
1005
1006 } elsif ($cur =~ /(^.)/o) {
1007 print "C($1)\n" if ($dbg_values > 1);
1008 }
1009 if (defined $1) {
1010 $cur = substr($cur, length($1));
1011 $res .= $type x length($1);
1012 }
1013 }
1014
1015 return ($res, $var);
1016 }
1017
1018 sub possible {
1019 my ($possible, $line) = @_;
1020 my $notPermitted = qr{(?:
1021 ^(?:
1022 $Modifier|
1023 $Storage|
1024 $Type|
1025 DEFINE_\S+
1026 )$|
1027 ^(?:
1028 goto|
1029 return|
1030 case|
1031 else|
1032 asm|__asm__|
1033 do
1034 )(?:\s|$)|
1035 ^(?:typedef|struct|enum)\b
1036 )}x;
1037 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1038 if ($possible !~ $notPermitted) {
1039 # Check for modifiers.
1040 $possible =~ s/\s*$Storage\s*//g;
1041 $possible =~ s/\s*$Sparse\s*//g;
1042 if ($possible =~ /^\s*$/) {
1043
1044 } elsif ($possible =~ /\s/) {
1045 $possible =~ s/\s*$Type\s*//g;
1046 for my $modifier (split(' ', $possible)) {
1047 if ($modifier !~ $notPermitted) {
1048 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1049 push(@modifierList, $modifier);
1050 }
1051 }
1052
1053 } else {
1054 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1055 push(@typeList, $possible);
1056 }
1057 build_types();
1058 } else {
1059 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1060 }
1061 }
1062
1063 my $prefix = '';
1064
1065 sub report {
1066 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1067 return 0;
1068 }
1069 my $line = $prefix . $_[0];
1070
1071 $line = (split('\n', $line))[0] . "\n" if ($terse);
1072
1073 push(our @report, $line);
1074
1075 return 1;
1076 }
1077 sub report_dump {
1078 our @report;
1079 }
1080 sub ERROR {
1081 if (report("ERROR: $_[0]\n")) {
1082 our $clean = 0;
1083 our $cnt_error++;
1084 }
1085 }
1086 sub WARN {
1087 if (report("WARNING: $_[0]\n")) {
1088 our $clean = 0;
1089 our $cnt_warn++;
1090 }
1091 }
1092 sub CHK {
1093 if ($check && report("CHECK: $_[0]\n")) {
1094 our $clean = 0;
1095 our $cnt_chk++;
1096 }
1097 }
1098
1099 sub check_absolute_file {
1100 my ($absolute, $herecurr) = @_;
1101 my $file = $absolute;
1102
1103 ##print "absolute<$absolute>\n";
1104
1105 # See if any suffix of this path is a path within the tree.
1106 while ($file =~ s@^[^/]*/@@) {
1107 if (-f "$root/$file") {
1108 ##print "file<$file>\n";
1109 last;
1110 }
1111 }
1112 if (! -f _) {
1113 return 0;
1114 }
1115
1116 # It is, so see if the prefix is acceptable.
1117 my $prefix = $absolute;
1118 substr($prefix, -length($file)) = '';
1119
1120 ##print "prefix<$prefix>\n";
1121 if ($prefix ne ".../") {
1122 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1123 }
1124 }
1125
1126 sub process {
1127 my $filename = shift;
1128
1129 my $linenr=0;
1130 my $prevline="";
1131 my $prevrawline="";
1132 my $stashline="";
1133 my $stashrawline="";
1134
1135 my $length;
1136 my $indent;
1137 my $previndent=0;
1138 my $stashindent=0;
1139
1140 our $clean = 1;
1141 my $signoff = 0;
1142 my $is_patch = 0;
1143
1144 our @report = ();
1145 our $cnt_lines = 0;
1146 our $cnt_error = 0;
1147 our $cnt_warn = 0;
1148 our $cnt_chk = 0;
1149
1150 # Trace the real file/line as we go.
1151 my $realfile = '';
1152 my $realline = 0;
1153 my $realcnt = 0;
1154 my $here = '';
1155 my $in_comment = 0;
1156 my $comment_edge = 0;
1157 my $first_line = 0;
1158 my $p1_prefix = '';
1159
1160 my $prev_values = 'E';
1161
1162 # suppression flags
1163 my %suppress_ifbraces;
1164 my %suppress_whiletrailers;
1165 my %suppress_export;
1166
1167 # Pre-scan the patch sanitizing the lines.
1168 # Pre-scan the patch looking for any __setup documentation.
1169 #
1170 my @setup_docs = ();
1171 my $setup_docs = 0;
1172
1173 sanitise_line_reset();
1174 my $line;
1175 foreach my $rawline (@rawlines) {
1176 $linenr++;
1177 $line = $rawline;
1178
1179 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1180 $setup_docs = 0;
1181 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1182 $setup_docs = 1;
1183 }
1184 #next;
1185 }
1186 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1187 $realline=$1-1;
1188 if (defined $2) {
1189 $realcnt=$3+1;
1190 } else {
1191 $realcnt=1+1;
1192 }
1193 $in_comment = 0;
1194
1195 # Guestimate if this is a continuing comment. Run
1196 # the context looking for a comment "edge". If this
1197 # edge is a close comment then we must be in a comment
1198 # at context start.
1199 my $edge;
1200 my $cnt = $realcnt;
1201 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1202 next if (defined $rawlines[$ln - 1] &&
1203 $rawlines[$ln - 1] =~ /^-/);
1204 $cnt--;
1205 #print "RAW<$rawlines[$ln - 1]>\n";
1206 last if (!defined $rawlines[$ln - 1]);
1207 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1208 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1209 ($edge) = $1;
1210 last;
1211 }
1212 }
1213 if (defined $edge && $edge eq '*/') {
1214 $in_comment = 1;
1215 }
1216
1217 # Guestimate if this is a continuing comment. If this
1218 # is the start of a diff block and this line starts
1219 # ' *' then it is very likely a comment.
1220 if (!defined $edge &&
1221 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1222 {
1223 $in_comment = 1;
1224 }
1225
1226 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1227 sanitise_line_reset($in_comment);
1228
1229 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1230 # Standardise the strings and chars within the input to
1231 # simplify matching -- only bother with positive lines.
1232 $line = sanitise_line($rawline);
1233 }
1234 push(@lines, $line);
1235
1236 if ($realcnt > 1) {
1237 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1238 } else {
1239 $realcnt = 0;
1240 }
1241
1242 #print "==>$rawline\n";
1243 #print "-->$line\n";
1244
1245 if ($setup_docs && $line =~ /^\+/) {
1246 push(@setup_docs, $line);
1247 }
1248 }
1249
1250 $prefix = '';
1251
1252 $realcnt = 0;
1253 $linenr = 0;
1254 foreach my $line (@lines) {
1255 $linenr++;
1256
1257 my $rawline = $rawlines[$linenr - 1];
1258
1259 #extract the line range in the file after the patch is applied
1260 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1261 $is_patch = 1;
1262 $first_line = $linenr + 1;
1263 $realline=$1-1;
1264 if (defined $2) {
1265 $realcnt=$3+1;
1266 } else {
1267 $realcnt=1+1;
1268 }
1269 annotate_reset();
1270 $prev_values = 'E';
1271
1272 %suppress_ifbraces = ();
1273 %suppress_whiletrailers = ();
1274 %suppress_export = ();
1275 next;
1276
1277 # track the line number as we move through the hunk, note that
1278 # new versions of GNU diff omit the leading space on completely
1279 # blank context lines so we need to count that too.
1280 } elsif ($line =~ /^( |\+|$)/) {
1281 $realline++;
1282 $realcnt-- if ($realcnt != 0);
1283
1284 # Measure the line length and indent.
1285 ($length, $indent) = line_stats($rawline);
1286
1287 # Track the previous line.
1288 ($prevline, $stashline) = ($stashline, $line);
1289 ($previndent, $stashindent) = ($stashindent, $indent);
1290 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1291
1292 #warn "line<$line>\n";
1293
1294 } elsif ($realcnt == 1) {
1295 $realcnt--;
1296 }
1297
1298 my $hunk_line = ($realcnt != 0);
1299
1300 #make up the handle for any error we report on this line
1301 $prefix = "$filename:$realline: " if ($emacs && $file);
1302 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1303
1304 $here = "#$linenr: " if (!$file);
1305 $here = "#$realline: " if ($file);
1306
1307 # extract the filename as it passes
1308 if ($line=~/^\+\+\+\s+(\S+)/) {
1309 $realfile = $1;
1310 $realfile =~ s@^([^/]*)/@@;
1311
1312 $p1_prefix = $1;
1313 if (!$file && $tree && $p1_prefix ne '' &&
1314 -e "$root/$p1_prefix") {
1315 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1316 }
1317
1318 if ($realfile =~ m@^include/asm/@) {
1319 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1320 }
1321 next;
1322 }
1323
1324 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1325
1326 my $hereline = "$here\n$rawline\n";
1327 my $herecurr = "$here\n$rawline\n";
1328 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1329
1330 $cnt_lines++ if ($realcnt != 0);
1331
1332 #check the patch for a signoff:
1333 if ($line =~ /^\s*signed-off-by:/i) {
1334 # This is a signoff, if ugly, so do not double report.
1335 $signoff++;
1336 if (!($line =~ /^\s*Signed-off-by:/)) {
1337 WARN("Signed-off-by: is the preferred form\n" .
1338 $herecurr);
1339 }
1340 if ($line =~ /^\s*signed-off-by:\S/i) {
1341 WARN("space required after Signed-off-by:\n" .
1342 $herecurr);
1343 }
1344 }
1345
1346 # Check for wrappage within a valid hunk of the file
1347 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1348 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1349 $herecurr) if (!$emitted_corrupt++);
1350 }
1351
1352 # Check for absolute kernel paths.
1353 if ($tree) {
1354 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1355 my $file = $1;
1356
1357 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1358 check_absolute_file($1, $herecurr)) {
1359 #
1360 } else {
1361 check_absolute_file($file, $herecurr);
1362 }
1363 }
1364 }
1365
1366 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1367 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1368 $rawline !~ m/^$UTF8*$/) {
1369 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1370
1371 my $blank = copy_spacing($rawline);
1372 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1373 my $hereptr = "$hereline$ptr\n";
1374
1375 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1376 }
1377
1378 # ignore non-hunk lines and lines being removed
1379 next if (!$hunk_line || $line =~ /^-/);
1380
1381 #trailing whitespace
1382 if ($line =~ /^\+.*\015/) {
1383 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1384 ERROR("DOS line endings\n" . $herevet);
1385
1386 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1387 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1388 ERROR("trailing whitespace\n" . $herevet);
1389 }
1390
1391 # check for Kconfig help text having a real description
1392 if ($realfile =~ /Kconfig/ &&
1393 $line =~ /\+?\s*(---)?help(---)?$/) {
1394 my $length = 0;
1395 for (my $l = $linenr; defined($lines[$l]); $l++) {
1396 my $f = $lines[$l];
1397 $f =~ s/#.*//;
1398 $f =~ s/^\s+//;
1399 next if ($f =~ /^$/);
1400 last if ($f =~ /^\s*config\s/);
1401 $length++;
1402 }
1403 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
1404 }
1405
1406 # check we are in a valid source file if not then ignore this hunk
1407 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1408
1409 #80 column limit
1410 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1411 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1412 $line !~ /^\+\s*$logFunctions\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1413 $length > 80)
1414 {
1415 WARN("line over 80 characters\n" . $herecurr);
1416 }
1417
1418 # check for spaces before a quoted newline
1419 if ($rawline =~ /^.*\".*\s\\n/) {
1420 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1421 }
1422
1423 # check for adding lines without a newline.
1424 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1425 WARN("adding a line without newline at end of file\n" . $herecurr);
1426 }
1427
1428 # Blackfin: use hi/lo macros
1429 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1430 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1431 my $herevet = "$here\n" . cat_vet($line) . "\n";
1432 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1433 }
1434 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1435 my $herevet = "$here\n" . cat_vet($line) . "\n";
1436 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1437 }
1438 }
1439
1440 # check we are in a valid source file C or perl if not then ignore this hunk
1441 next if ($realfile !~ /\.(h|c|pl)$/);
1442
1443 # at the beginning of a line any tabs must come first and anything
1444 # more than 8 must use tabs.
1445 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1446 $rawline =~ /^\+\s* \s*/) {
1447 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1448 ERROR("code indent should use tabs where possible\n" . $herevet);
1449 }
1450
1451 # check for space before tabs.
1452 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1453 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1454 WARN("please, no space before tabs\n" . $herevet);
1455 }
1456
1457 # check for spaces at the beginning of a line.
1458 if ($rawline =~ /^\+ / && $rawline !~ /\+ +\*/) {
1459 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1460 WARN("please, no space for starting a line, \
1461 excluding comments\n" . $herevet);
1462 }
1463
1464 # check we are in a valid C source file if not then ignore this hunk
1465 next if ($realfile !~ /\.(h|c)$/);
1466
1467 # check for RCS/CVS revision markers
1468 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1469 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1470 }
1471
1472 # Blackfin: don't use __builtin_bfin_[cs]sync
1473 if ($line =~ /__builtin_bfin_csync/) {
1474 my $herevet = "$here\n" . cat_vet($line) . "\n";
1475 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1476 }
1477 if ($line =~ /__builtin_bfin_ssync/) {
1478 my $herevet = "$here\n" . cat_vet($line) . "\n";
1479 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1480 }
1481
1482 # Check for potential 'bare' types
1483 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1484 $realline_next);
1485 if ($realcnt && $line =~ /.\s*\S/) {
1486 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1487 ctx_statement_block($linenr, $realcnt, 0);
1488 $stat =~ s/\n./\n /g;
1489 $cond =~ s/\n./\n /g;
1490
1491 # Find the real next line.
1492 $realline_next = $line_nr_next;
1493 if (defined $realline_next &&
1494 (!defined $lines[$realline_next - 1] ||
1495 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1496 $realline_next++;
1497 }
1498
1499 my $s = $stat;
1500 $s =~ s/{.*$//s;
1501
1502 # Ignore goto labels.
1503 if ($s =~ /$Ident:\*$/s) {
1504
1505 # Ignore functions being called
1506 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1507
1508 } elsif ($s =~ /^.\s*else\b/s) {
1509
1510 # declarations always start with types
1511 } 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) {
1512 my $type = $1;
1513 $type =~ s/\s+/ /g;
1514 possible($type, "A:" . $s);
1515
1516 # definitions in global scope can only start with types
1517 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1518 possible($1, "B:" . $s);
1519 }
1520
1521 # any (foo ... *) is a pointer cast, and foo is a type
1522 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1523 possible($1, "C:" . $s);
1524 }
1525
1526 # Check for any sort of function declaration.
1527 # int foo(something bar, other baz);
1528 # void (*store_gdt)(x86_descr_ptr *);
1529 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1530 my ($name_len) = length($1);
1531
1532 my $ctx = $s;
1533 substr($ctx, 0, $name_len + 1, '');
1534 $ctx =~ s/\)[^\)]*$//;
1535
1536 for my $arg (split(/\s*,\s*/, $ctx)) {
1537 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1538
1539 possible($1, "D:" . $s);
1540 }
1541 }
1542 }
1543
1544 }
1545
1546 #
1547 # Checks which may be anchored in the context.
1548 #
1549
1550 # Check for switch () and associated case and default
1551 # statements should be at the same indent.
1552 if ($line=~/\bswitch\s*\(.*\)/) {
1553 my $err = '';
1554 my $sep = '';
1555 my @ctx = ctx_block_outer($linenr, $realcnt);
1556 shift(@ctx);
1557 for my $ctx (@ctx) {
1558 my ($clen, $cindent) = line_stats($ctx);
1559 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1560 $indent != $cindent) {
1561 $err .= "$sep$ctx\n";
1562 $sep = '';
1563 } else {
1564 $sep = "[...]\n";
1565 }
1566 }
1567 if ($err ne '') {
1568 ERROR("switch and case should be at the same indent\n$hereline$err");
1569 }
1570 }
1571
1572 # if/while/etc brace do not go on next line, unless defining a do while loop,
1573 # or if that brace on the next line is for something else
1574 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1575 my $pre_ctx = "$1$2";
1576
1577 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1578 my $ctx_cnt = $realcnt - $#ctx - 1;
1579 my $ctx = join("\n", @ctx);
1580
1581 my $ctx_ln = $linenr;
1582 my $ctx_skip = $realcnt;
1583
1584 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1585 defined $lines[$ctx_ln - 1] &&
1586 $lines[$ctx_ln - 1] =~ /^-/)) {
1587 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1588 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1589 $ctx_ln++;
1590 }
1591
1592 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1593 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1594
1595 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1596 ERROR("that open brace { should be on the previous line\n" .
1597 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1598 }
1599 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1600 $ctx =~ /\)\s*\;\s*$/ &&
1601 defined $lines[$ctx_ln - 1])
1602 {
1603 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1604 if ($nindent > $indent) {
1605 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1606 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1607 }
1608 }
1609 }
1610
1611 # Check relative indent for conditionals and blocks.
1612 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1613 my ($s, $c) = ($stat, $cond);
1614
1615 substr($s, 0, length($c), '');
1616
1617 # Make sure we remove the line prefixes as we have
1618 # none on the first line, and are going to readd them
1619 # where necessary.
1620 $s =~ s/\n./\n/gs;
1621
1622 # Find out how long the conditional actually is.
1623 my @newlines = ($c =~ /\n/gs);
1624 my $cond_lines = 1 + $#newlines;
1625
1626 # We want to check the first line inside the block
1627 # starting at the end of the conditional, so remove:
1628 # 1) any blank line termination
1629 # 2) any opening brace { on end of the line
1630 # 3) any do (...) {
1631 my $continuation = 0;
1632 my $check = 0;
1633 $s =~ s/^.*\bdo\b//;
1634 $s =~ s/^\s*{//;
1635 if ($s =~ s/^\s*\\//) {
1636 $continuation = 1;
1637 }
1638 if ($s =~ s/^\s*?\n//) {
1639 $check = 1;
1640 $cond_lines++;
1641 }
1642
1643 # Also ignore a loop construct at the end of a
1644 # preprocessor statement.
1645 if (($prevline =~ /^.\s*#\s*define\s/ ||
1646 $prevline =~ /\\\s*$/) && $continuation == 0) {
1647 $check = 0;
1648 }
1649
1650 my $cond_ptr = -1;
1651 $continuation = 0;
1652 while ($cond_ptr != $cond_lines) {
1653 $cond_ptr = $cond_lines;
1654
1655 # If we see an #else/#elif then the code
1656 # is not linear.
1657 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1658 $check = 0;
1659 }
1660
1661 # Ignore:
1662 # 1) blank lines, they should be at 0,
1663 # 2) preprocessor lines, and
1664 # 3) labels.
1665 if ($continuation ||
1666 $s =~ /^\s*?\n/ ||
1667 $s =~ /^\s*#\s*?/ ||
1668 $s =~ /^\s*$Ident\s*:/) {
1669 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1670 if ($s =~ s/^.*?\n//) {
1671 $cond_lines++;
1672 }
1673 }
1674 }
1675
1676 my (undef, $sindent) = line_stats("+" . $s);
1677 my $stat_real = raw_line($linenr, $cond_lines);
1678
1679 # Check if either of these lines are modified, else
1680 # this is not this patch's fault.
1681 if (!defined($stat_real) ||
1682 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1683 $check = 0;
1684 }
1685 if (defined($stat_real) && $cond_lines > 1) {
1686 $stat_real = "[...]\n$stat_real";
1687 }
1688
1689 #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";
1690
1691 if ($check && (($sindent % 8) != 0 ||
1692 ($sindent <= $indent && $s ne ''))) {
1693 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1694 }
1695 }
1696
1697 # Track the 'values' across context and added lines.
1698 my $opline = $line; $opline =~ s/^./ /;
1699 my ($curr_values, $curr_vars) =
1700 annotate_values($opline . "\n", $prev_values);
1701 $curr_values = $prev_values . $curr_values;
1702 if ($dbg_values) {
1703 my $outline = $opline; $outline =~ s/\t/ /g;
1704 print "$linenr > .$outline\n";
1705 print "$linenr > $curr_values\n";
1706 print "$linenr > $curr_vars\n";
1707 }
1708 $prev_values = substr($curr_values, -1);
1709
1710 #ignore lines not being added
1711 if ($line=~/^[^\+]/) {next;}
1712
1713 # TEST: allow direct testing of the type matcher.
1714 if ($dbg_type) {
1715 if ($line =~ /^.\s*$Declare\s*$/) {
1716 ERROR("TEST: is type\n" . $herecurr);
1717 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1718 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1719 }
1720 next;
1721 }
1722 # TEST: allow direct testing of the attribute matcher.
1723 if ($dbg_attr) {
1724 if ($line =~ /^.\s*$Modifier\s*$/) {
1725 ERROR("TEST: is attr\n" . $herecurr);
1726 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1727 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1728 }
1729 next;
1730 }
1731
1732 # check for initialisation to aggregates open brace on the next line
1733 if ($line =~ /^.\s*{/ &&
1734 $prevline =~ /(?:^|[^=])=\s*$/) {
1735 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1736 }
1737
1738 #
1739 # Checks which are anchored on the added line.
1740 #
1741
1742 # check for malformed paths in #include statements (uses RAW line)
1743 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1744 my $path = $1;
1745 if ($path =~ m{//}) {
1746 ERROR("malformed #include filename\n" .
1747 $herecurr);
1748 }
1749 }
1750
1751 # no C99 // comments
1752 if ($line =~ m{//}) {
1753 ERROR("do not use C99 // comments\n" . $herecurr);
1754 }
1755 # Remove C99 comments.
1756 $line =~ s@//.*@@;
1757 $opline =~ s@//.*@@;
1758
1759 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1760 # the whole statement.
1761 #print "APW <$lines[$realline_next - 1]>\n";
1762 if (defined $realline_next &&
1763 exists $lines[$realline_next - 1] &&
1764 !defined $suppress_export{$realline_next} &&
1765 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1766 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1767 my $name = $1;
1768 if ($stat !~ /(?:
1769 \n.}\s*$|
1770 ^.DEFINE_$Ident\(\Q$name\E\)|
1771 ^.DECLARE_$Ident\(\Q$name\E\)|
1772 ^.LIST_HEAD\(\Q$name\E\)|
1773 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1774 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1775 )/x) {
1776 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1777 $suppress_export{$realline_next} = 2;
1778 } else {
1779 $suppress_export{$realline_next} = 1;
1780 }
1781 }
1782 if (!defined $suppress_export{$linenr} &&
1783 $prevline =~ /^.\s*$/ &&
1784 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1785 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1786 #print "FOO B <$lines[$linenr - 1]>\n";
1787 $suppress_export{$linenr} = 2;
1788 }
1789 if (defined $suppress_export{$linenr} &&
1790 $suppress_export{$linenr} == 2) {
1791 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1792 }
1793
1794 # check for external initialisers.
1795 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1796 ERROR("do not initialise externals to 0 or NULL\n" .
1797 $herecurr);
1798 }
1799 # check for static initialisers.
1800 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1801 ERROR("do not initialise statics to 0 or NULL\n" .
1802 $herecurr);
1803 }
1804
1805 # check for new typedefs, only function parameters and sparse annotations
1806 # make sense.
1807 if ($line =~ /\btypedef\s/ &&
1808 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1809 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1810 $line !~ /\b$typeTypedefs\b/ &&
1811 $line !~ /\b__bitwise(?:__|)\b/) {
1812 WARN("do not add new typedefs\n" . $herecurr);
1813 }
1814
1815 # * goes on variable not on type
1816 # (char*[ const])
1817 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1818 my ($from, $to) = ($1, $1);
1819
1820 # Should start with a space.
1821 $to =~ s/^(\S)/ $1/;
1822 # Should not end with a space.
1823 $to =~ s/\s+$//;
1824 # '*'s should not have spaces between.
1825 while ($to =~ s/\*\s+\*/\*\*/) {
1826 }
1827
1828 #print "from<$from> to<$to>\n";
1829 if ($from ne $to) {
1830 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1831 }
1832 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1833 my ($from, $to, $ident) = ($1, $1, $2);
1834
1835 # Should start with a space.
1836 $to =~ s/^(\S)/ $1/;
1837 # Should not end with a space.
1838 $to =~ s/\s+$//;
1839 # '*'s should not have spaces between.
1840 while ($to =~ s/\*\s+\*/\*\*/) {
1841 }
1842 # Modifiers should have spaces.
1843 $to =~ s/(\b$Modifier$)/$1 /;
1844
1845 #print "from<$from> to<$to> ident<$ident>\n";
1846 if ($from ne $to && $ident !~ /^$Modifier$/) {
1847 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1848 }
1849 }
1850
1851 # # no BUG() or BUG_ON()
1852 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1853 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1854 # print "$herecurr";
1855 # $clean = 0;
1856 # }
1857
1858 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1859 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1860 }
1861
1862 # printk should use KERN_* levels. Note that follow on printk's on the
1863 # same line do not need a level, so we use the current block context
1864 # to try and find and validate the current printk. In summary the current
1865 # printk includes all preceeding printk's which have no newline on the end.
1866 # we assume the first bad printk is the one to report.
1867 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1868 my $ok = 0;
1869 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1870 #print "CHECK<$lines[$ln - 1]\n";
1871 # we have a preceeding printk if it ends
1872 # with "\n" ignore it, else it is to blame
1873 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1874 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1875 $ok = 1;
1876 }
1877 last;
1878 }
1879 }
1880 if ($ok == 0) {
1881 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1882 }
1883 }
1884
1885 # function brace can't be on same line, except for #defines of do while,
1886 # or if closed on same line
1887 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1888 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1889 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1890 }
1891
1892 # open braces for enum, union and struct go on the same line.
1893 if ($line =~ /^.\s*{/ &&
1894 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1895 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1896 }
1897
1898 # check for spacing round square brackets; allowed:
1899 # 1. with a type on the left -- int [] a;
1900 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1901 # 3. inside a curly brace -- = { [0...10] = 5 }
1902 while ($line =~ /(.*?\s)\[/g) {
1903 my ($where, $prefix) = ($-[1], $1);
1904 if ($prefix !~ /$Type\s+$/ &&
1905 ($where != 0 || $prefix !~ /^.\s+$/) &&
1906 $prefix !~ /{\s+$/) {
1907 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1908 }
1909 }
1910
1911 # check for spaces between functions and their parentheses.
1912 while ($line =~ /($Ident)\s+\(/g) {
1913 my $name = $1;
1914 my $ctx_before = substr($line, 0, $-[1]);
1915 my $ctx = "$ctx_before$name";
1916
1917 # Ignore those directives where spaces _are_ permitted.
1918 if ($name =~ /^(?:
1919 if|for|while|switch|return|case|
1920 volatile|__volatile__|
1921 __attribute__|format|__extension__|
1922 asm|__asm__)$/x)
1923 {
1924
1925 # cpp #define statements have non-optional spaces, ie
1926 # if there is a space between the name and the open
1927 # parenthesis it is simply not a parameter group.
1928 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1929
1930 # cpp #elif statement condition may start with a (
1931 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1932
1933 # If this whole things ends with a type its most
1934 # likely a typedef for a function.
1935 } elsif ($ctx =~ /$Type$/) {
1936
1937 } else {
1938 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1939 }
1940 }
1941 # Check operator spacing.
1942 if (!($line=~/\#\s*include/)) {
1943 my $ops = qr{
1944 <<=|>>=|<=|>=|==|!=|
1945 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1946 =>|->|<<|>>|<|>|=|!|~|
1947 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1948 \?|:
1949 }x;
1950 my @elements = split(/($ops|;)/, $opline);
1951 my $off = 0;
1952
1953 my $blank = copy_spacing($opline);
1954
1955 for (my $n = 0; $n < $#elements; $n += 2) {
1956 $off += length($elements[$n]);
1957
1958 # Pick up the preceeding and succeeding characters.
1959 my $ca = substr($opline, 0, $off);
1960 my $cc = '';
1961 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1962 $cc = substr($opline, $off + length($elements[$n + 1]));
1963 }
1964 my $cb = "$ca$;$cc";
1965
1966 my $a = '';
1967 $a = 'V' if ($elements[$n] ne '');
1968 $a = 'W' if ($elements[$n] =~ /\s$/);
1969 $a = 'C' if ($elements[$n] =~ /$;$/);
1970 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1971 $a = 'O' if ($elements[$n] eq '');
1972 $a = 'E' if ($ca =~ /^\s*$/);
1973
1974 my $op = $elements[$n + 1];
1975
1976 my $c = '';
1977 if (defined $elements[$n + 2]) {
1978 $c = 'V' if ($elements[$n + 2] ne '');
1979 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1980 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1981 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1982 $c = 'O' if ($elements[$n + 2] eq '');
1983 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1984 } else {
1985 $c = 'E';
1986 }
1987
1988 my $ctx = "${a}x${c}";
1989
1990 my $at = "(ctx:$ctx)";
1991
1992 my $ptr = substr($blank, 0, $off) . "^";
1993 my $hereptr = "$hereline$ptr\n";
1994
1995 # Pull out the value of this operator.
1996 my $op_type = substr($curr_values, $off + 1, 1);
1997
1998 # Get the full operator variant.
1999 my $opv = $op . substr($curr_vars, $off, 1);
2000
2001 # Ignore operators passed as parameters.
2002 if ($op_type ne 'V' &&
2003 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2004
2005 # # Ignore comments
2006 # } elsif ($op =~ /^$;+$/) {
2007
2008 # ; should have either the end of line or a space or \ after it
2009 } elsif ($op eq ';') {
2010 if ($ctx !~ /.x[WEBC]/ &&
2011 $cc !~ /^\\/ && $cc !~ /^;/) {
2012 ERROR("space required after that '$op' $at\n" . $hereptr);
2013 }
2014
2015 # // is a comment
2016 } elsif ($op eq '//') {
2017
2018 # No spaces for:
2019 # ->
2020 # : when part of a bitfield
2021 } elsif ($op eq '->' || $opv eq ':B') {
2022 if ($ctx =~ /Wx.|.xW/) {
2023 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2024 }
2025
2026 # , must have a space on the right.
2027 } elsif ($op eq ',') {
2028 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2029 ERROR("space required after that '$op' $at\n" . $hereptr);
2030 }
2031
2032 # '*' as part of a type definition -- reported already.
2033 } elsif ($opv eq '*_') {
2034 #warn "'*' is part of type\n";
2035
2036 # unary operators should have a space before and
2037 # none after. May be left adjacent to another
2038 # unary operator, or a cast
2039 } elsif ($op eq '!' || $op eq '~' ||
2040 $opv eq '*U' || $opv eq '-U' ||
2041 $opv eq '&U' || $opv eq '&&U') {
2042 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2043 ERROR("space required before that '$op' $at\n" . $hereptr);
2044 }
2045 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2046 # A unary '*' may be const
2047
2048 } elsif ($ctx =~ /.xW/) {
2049 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2050 }
2051
2052 # unary ++ and unary -- are allowed no space on one side.
2053 } elsif ($op eq '++' or $op eq '--') {
2054 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2055 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2056 }
2057 if ($ctx =~ /Wx[BE]/ ||
2058 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2059 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2060 }
2061 if ($ctx =~ /ExW/) {
2062 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2063 }
2064
2065
2066 # << and >> may either have or not have spaces both sides
2067 } elsif ($op eq '<<' or $op eq '>>' or
2068 $op eq '&' or $op eq '^' or $op eq '|' or
2069 $op eq '+' or $op eq '-' or
2070 $op eq '*' or $op eq '/' or
2071 $op eq '%')
2072 {
2073 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2074 ERROR("need consistent spacing around '$op' $at\n" .
2075 $hereptr);
2076 }
2077
2078 # A colon needs no spaces before when it is
2079 # terminating a case value or a label.
2080 } elsif ($opv eq ':C' || $opv eq ':L') {
2081 if ($ctx =~ /Wx./) {
2082 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2083 }
2084
2085 # All the others need spaces both sides.
2086 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2087 my $ok = 0;
2088
2089 # Ignore email addresses <foo@bar>
2090 if (($op eq '<' &&
2091 $cc =~ /^\S+\@\S+>/) ||
2092 ($op eq '>' &&
2093 $ca =~ /<\S+\@\S+$/))
2094 {
2095 $ok = 1;
2096 }
2097
2098 # Ignore ?:
2099 if (($opv eq ':O' && $ca =~ /\?$/) ||
2100 ($op eq '?' && $cc =~ /^:/)) {
2101 $ok = 1;
2102 }
2103
2104 if ($ok == 0) {
2105 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2106 }
2107 }
2108 $off += length($elements[$n + 1]);
2109 }
2110 }
2111
2112 # check for multiple assignments
2113 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2114 CHK("multiple assignments should be avoided\n" . $herecurr);
2115 }
2116
2117 ## # check for multiple declarations, allowing for a function declaration
2118 ## # continuation.
2119 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2120 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2121 ##
2122 ## # Remove any bracketed sections to ensure we do not
2123 ## # falsly report the parameters of functions.
2124 ## my $ln = $line;
2125 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2126 ## }
2127 ## if ($ln =~ /,/) {
2128 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2129 ## }
2130 ## }
2131
2132 #need space before brace following if, while, etc
2133 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2134 $line =~ /do{/) {
2135 ERROR("space required before the open brace '{'\n" . $herecurr);
2136 }
2137
2138 # closing brace should have a space following it when it has anything
2139 # on the line
2140 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2141 ERROR("space required after that close brace '}'\n" . $herecurr);
2142 }
2143
2144 # check spacing on square brackets
2145 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2146 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2147 }
2148 if ($line =~ /\s\]/) {
2149 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2150 }
2151
2152 # check spacing on parentheses
2153 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2154 $line !~ /for\s*\(\s+;/) {
2155 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2156 }
2157 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2158 $line !~ /for\s*\(.*;\s+\)/ &&
2159 $line !~ /:\s+\)/) {
2160 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2161 }
2162
2163 #goto labels aren't indented, allow a single space however
2164 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2165 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2166 WARN("labels should not be indented\n" . $herecurr);
2167 }
2168
2169 # Return is not a function.
2170 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2171 my $spacing = $1;
2172 my $value = $2;
2173
2174 # Flatten any parentheses
2175 $value =~ s/\)\(/\) \(/g;
2176 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2177 $value !~ /(?:$Ident|-?$Constant)\s*
2178 $Compare\s*
2179 (?:$Ident|-?$Constant)/x &&
2180 $value =~ s/\([^\(\)]*\)/1/) {
2181 }
2182
2183 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2184 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2185
2186 } elsif ($spacing !~ /\s+/) {
2187 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2188 }
2189 }
2190
2191 # Need a space before open parenthesis after if, while etc
2192 if ($line=~/\b(if|while|for|switch)\(/) {
2193 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2194 }
2195
2196 # Check for illegal assignment in if conditional -- and check for trailing
2197 # statements after the conditional.
2198 if ($line =~ /do\s*(?!{)/) {
2199 my ($stat_next) = ctx_statement_block($line_nr_next,
2200 $remain_next, $off_next);
2201 $stat_next =~ s/\n./\n /g;
2202 ##print "stat<$stat> stat_next<$stat_next>\n";
2203
2204 if ($stat_next =~ /^\s*while\b/) {
2205 # If the statement carries leading newlines,
2206 # then count those as offsets.
2207 my ($whitespace) =
2208 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2209 my $offset =
2210 statement_rawlines($whitespace) - 1;
2211
2212 $suppress_whiletrailers{$line_nr_next +
2213 $offset} = 1;
2214 }
2215 }
2216 if (!defined $suppress_whiletrailers{$linenr} &&
2217 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2218 my ($s, $c) = ($stat, $cond);
2219
2220 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2221 ERROR("do not use assignment in if condition\n" . $herecurr);
2222 }
2223
2224 # Find out what is on the end of the line after the
2225 # conditional.
2226 substr($s, 0, length($c), '');
2227 $s =~ s/\n.*//g;
2228 $s =~ s/$;//g; # Remove any comments
2229 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2230 $c !~ /}\s*while\s*/)
2231 {
2232 # Find out how long the conditional actually is.
2233 my @newlines = ($c =~ /\n/gs);
2234 my $cond_lines = 1 + $#newlines;
2235 my $stat_real = '';
2236
2237 $stat_real = raw_line($linenr, $cond_lines)
2238 . "\n" if ($cond_lines);
2239 if (defined($stat_real) && $cond_lines > 1) {
2240 $stat_real = "[...]\n$stat_real";
2241 }
2242
2243 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2244 }
2245 }
2246
2247 # Check for bitwise tests written as boolean
2248 if ($line =~ /
2249 (?:
2250 (?:\[|\(|\&\&|\|\|)
2251 \s*0[xX][0-9]+\s*
2252 (?:\&\&|\|\|)
2253 |
2254 (?:\&\&|\|\|)
2255 \s*0[xX][0-9]+\s*
2256 (?:\&\&|\|\||\)|\])
2257 )/x)
2258 {
2259 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2260 }
2261
2262 # if and else should not have general statements after it
2263 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2264 my $s = $1;
2265 $s =~ s/$;//g; # Remove any comments
2266 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2267 ERROR("trailing statements should be on next line\n" . $herecurr);
2268 }
2269 }
2270 # if should not continue a brace
2271 if ($line =~ /}\s*if\b/) {
2272 ERROR("trailing statements should be on next line\n" .
2273 $herecurr);
2274 }
2275 # case and default should not have general statements after them
2276 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2277 $line !~ /\G(?:
2278 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2279 \s*return\s+
2280 )/xg)
2281 {
2282 ERROR("trailing statements should be on next line\n" . $herecurr);
2283 }
2284
2285 # Check for }<nl>else {, these must be at the same
2286 # indent level to be relevant to each other.
2287 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2288 $previndent == $indent) {
2289 ERROR("else should follow close brace '}'\n" . $hereprev);
2290 }
2291
2292 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2293 $previndent == $indent) {
2294 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2295
2296 # Find out what is on the end of the line after the
2297 # conditional.
2298 substr($s, 0, length($c), '');
2299 $s =~ s/\n.*//g;
2300
2301 if ($s =~ /^\s*;/) {
2302 ERROR("while should follow close brace '}'\n" . $hereprev);
2303 }
2304 }
2305
2306 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2307 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2308 # print "No studly caps, use _\n";
2309 # print "$herecurr";
2310 # $clean = 0;
2311 # }
2312
2313 #no spaces allowed after \ in define
2314 if ($line=~/\#\s*define.*\\\s$/) {
2315 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2316 }
2317
2318 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2319 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2320 my $file = "$1.h";
2321 my $checkfile = "include/linux/$file";
2322 if (-f "$root/$checkfile" &&
2323 $realfile ne $checkfile &&
2324 $1 !~ /$allowed_asm_includes/)
2325 {
2326 if ($realfile =~ m{^arch/}) {
2327 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2328 } else {
2329 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2330 }
2331 }
2332 }
2333
2334 # multi-statement macros should be enclosed in a do while loop, grab the
2335 # first statement and ensure its the whole macro if its not enclosed
2336 # in a known good container
2337 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2338 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2339 my $ln = $linenr;
2340 my $cnt = $realcnt;
2341 my ($off, $dstat, $dcond, $rest);
2342 my $ctx = '';
2343
2344 my $args = defined($1);
2345
2346 # Find the end of the macro and limit our statement
2347 # search to that.
2348 while ($cnt > 0 && defined $lines[$ln - 1] &&
2349 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2350 {
2351 $ctx .= $rawlines[$ln - 1] . "\n";
2352 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2353 $ln++;
2354 }
2355 $ctx .= $rawlines[$ln - 1];
2356
2357 ($dstat, $dcond, $ln, $cnt, $off) =
2358 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2359 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2360 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2361
2362 # Extract the remainder of the define (if any) and
2363 # rip off surrounding spaces, and trailing \'s.
2364 $rest = '';
2365 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2366 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2367 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2368 $rest .= substr($lines[$ln - 1], $off) . "\n";
2369 $cnt--;
2370 }
2371 $ln++;
2372 $off = 0;
2373 }
2374 $rest =~ s/\\\n.//g;
2375 $rest =~ s/^\s*//s;
2376 $rest =~ s/\s*$//s;
2377
2378 # Clean up the original statement.
2379 if ($args) {
2380 substr($dstat, 0, length($dcond), '');
2381 } else {
2382 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2383 }
2384 $dstat =~ s/$;//g;
2385 $dstat =~ s/\\\n.//g;
2386 $dstat =~ s/^\s*//s;
2387 $dstat =~ s/\s*$//s;
2388
2389 # Flatten any parentheses and braces
2390 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2391 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2392 $dstat =~ s/\[[^\{\}]*\]/1/)
2393 {
2394 }
2395
2396 my $exceptions = qr{
2397 $Declare|
2398 module_param_named|
2399 MODULE_PARAM_DESC|
2400 DECLARE_PER_CPU|
2401 DEFINE_PER_CPU|
2402 __typeof__\(|
2403 union|
2404 struct|
2405 \.$Ident\s*=\s*|
2406 ^\"|\"$
2407 }x;
2408 #print "REST<$rest> dstat<$dstat>\n";
2409 if ($rest ne '') {
2410 if ($rest !~ /while\s*\(/ &&
2411 $dstat !~ /$exceptions/)
2412 {
2413 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2414 }
2415
2416 } elsif ($ctx !~ /;/) {
2417 if ($dstat ne '' &&
2418 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2419 $dstat !~ /$exceptions/ &&
2420 $dstat !~ /^\.$Ident\s*=/ &&
2421 $dstat =~ /$Operators/)
2422 {
2423 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2424 }
2425 }
2426 }
2427
2428 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2429 # all assignments may have only one of the following with an assignment:
2430 # .
2431 # ALIGN(...)
2432 # VMLINUX_SYMBOL(...)
2433 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2434 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2435 }
2436
2437 # check for redundant bracing round if etc
2438 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2439 my ($level, $endln, @chunks) =
2440 ctx_statement_full($linenr, $realcnt, 1);
2441 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2442 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2443 if ($#chunks > 0 && $level == 0) {
2444 my $allowed = 0;
2445 my $seen = 0;
2446 my $herectx = $here . "\n";
2447 my $ln = $linenr - 1;
2448 for my $chunk (@chunks) {
2449 my ($cond, $block) = @{$chunk};
2450
2451 # If the condition carries leading newlines, then count those as offsets.
2452 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2453 my $offset = statement_rawlines($whitespace) - 1;
2454
2455 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2456
2457 # We have looked at and allowed this specific line.
2458 $suppress_ifbraces{$ln + $offset} = 1;
2459
2460 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2461 $ln += statement_rawlines($block) - 1;
2462
2463 substr($block, 0, length($cond), '');
2464
2465 $seen++ if ($block =~ /^\s*{/);
2466
2467 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2468 if (statement_lines($cond) > 1) {
2469 #print "APW: ALLOWED: cond<$cond>\n";
2470 $allowed = 1;
2471 }
2472 if ($block =~/\b(?:if|for|while)\b/) {
2473 #print "APW: ALLOWED: block<$block>\n";
2474 $allowed = 1;
2475 }
2476 if (statement_block_size($block) > 1) {
2477 #print "APW: ALLOWED: lines block<$block>\n";
2478 $allowed = 1;
2479 }
2480 }
2481 if ($seen && !$allowed) {
2482 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2483 }
2484 }
2485 }
2486 if (!defined $suppress_ifbraces{$linenr - 1} &&
2487 $line =~ /\b(if|while|for|else)\b/) {
2488 my $allowed = 0;
2489
2490 # Check the pre-context.
2491 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2492 #print "APW: ALLOWED: pre<$1>\n";
2493 $allowed = 1;
2494 }
2495
2496 my ($level, $endln, @chunks) =
2497 ctx_statement_full($linenr, $realcnt, $-[0]);
2498
2499 # Check the condition.
2500 my ($cond, $block) = @{$chunks[0]};
2501 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2502 if (defined $cond) {
2503 substr($block, 0, length($cond), '');
2504 }
2505 if (statement_lines($cond) > 1) {
2506 #print "APW: ALLOWED: cond<$cond>\n";
2507 $allowed = 1;
2508 }
2509 if ($block =~/\b(?:if|for|while)\b/) {
2510 #print "APW: ALLOWED: block<$block>\n";
2511 $allowed = 1;
2512 }
2513 if (statement_block_size($block) > 1) {
2514 #print "APW: ALLOWED: lines block<$block>\n";
2515 $allowed = 1;
2516 }
2517 # Check the post-context.
2518 if (defined $chunks[1]) {
2519 my ($cond, $block) = @{$chunks[1]};
2520 if (defined $cond) {
2521 substr($block, 0, length($cond), '');
2522 }
2523 if ($block =~ /^\s*\{/) {
2524 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2525 $allowed = 1;
2526 }
2527 }
2528 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2529 my $herectx = $here . "\n";;
2530 my $cnt = statement_rawlines($block);
2531
2532 for (my $n = 0; $n < $cnt; $n++) {
2533 $herectx .= raw_line($linenr, $n) . "\n";;
2534 }
2535
2536 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2537 }
2538 }
2539
2540 # don't include deprecated include files (uses RAW line)
2541 for my $inc (@dep_includes) {
2542 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2543 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2544 }
2545 }
2546
2547 # don't use deprecated functions
2548 for my $func (@dep_functions) {
2549 if ($line =~ /\b$func\b/) {
2550 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2551 }
2552 }
2553
2554 # no volatiles please
2555 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2556 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2557 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2558 }
2559
2560 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2561 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2562 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2563 }
2564
2565 # warn about #if 0
2566 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2567 CHK("if this code is redundant consider removing it\n" .
2568 $herecurr);
2569 }
2570
2571 # check for needless kfree() checks
2572 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2573 my $expr = $1;
2574 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2575 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2576 }
2577 }
2578 # check for needless usb_free_urb() checks
2579 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2580 my $expr = $1;
2581 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2582 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2583 }
2584 }
2585
2586 # warn about #ifdefs in C files
2587 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2588 # print "#ifdef in C files should be avoided\n";
2589 # print "$herecurr";
2590 # $clean = 0;
2591 # }
2592
2593 # warn about spacing in #ifdefs
2594 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2595 ERROR("exactly one space required after that #$1\n" . $herecurr);
2596 }
2597
2598 # check for spinlock_t definitions without a comment.
2599 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2600 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2601 my $which = $1;
2602 if (!ctx_has_comment($first_line, $linenr)) {
2603 CHK("$1 definition without comment\n" . $herecurr);
2604 }
2605 }
2606 # check for memory barriers without a comment.
2607 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2608 if (!ctx_has_comment($first_line, $linenr)) {
2609 CHK("memory barrier without comment\n" . $herecurr);
2610 }
2611 }
2612 # check of hardware specific defines
2613 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2614 CHK("architecture specific defines should be avoided\n" . $herecurr);
2615 }
2616
2617 # Check that the storage class is at the beginning of a declaration
2618 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2619 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2620 }
2621
2622 # check the location of the inline attribute, that it is between
2623 # storage class and type.
2624 if ($line =~ /\b$Type\s+$Inline\b/ ||
2625 $line =~ /\b$Inline\s+$Storage\b/) {
2626 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2627 }
2628
2629 # Check for __inline__ and __inline, prefer inline
2630 if ($line =~ /\b(__inline__|__inline)\b/) {
2631 WARN("plain inline is preferred over $1\n" . $herecurr);
2632 }
2633
2634 # check for sizeof(&)
2635 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2636 WARN("sizeof(& should be avoided\n" . $herecurr);
2637 }
2638
2639 # check for new externs in .c files.
2640 if ($realfile =~ /\.c$/ && defined $stat &&
2641 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2642 {
2643 my $function_name = $1;
2644 my $paren_space = $2;
2645
2646 my $s = $stat;
2647 if (defined $cond) {
2648 substr($s, 0, length($cond), '');
2649 }
2650 if ($s =~ /^\s*;/ &&
2651 $function_name ne 'uninitialized_var')
2652 {
2653 WARN("externs should be avoided in .c files\n" . $herecurr);
2654 }
2655
2656 if ($paren_space =~ /\n/) {
2657 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2658 }
2659
2660 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2661 $stat =~ /^.\s*extern\s+/)
2662 {
2663 WARN("externs should be avoided in .c files\n" . $herecurr);
2664 }
2665
2666 # checks for new __setup's
2667 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2668 my $name = $1;
2669
2670 if (!grep(/$name/, @setup_docs)) {
2671 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2672 }
2673 }
2674
2675 # check for pointless casting of kmalloc return
2676 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2677 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2678 }
2679
2680 # check for gcc specific __FUNCTION__
2681 if ($line =~ /__FUNCTION__/) {
2682 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2683 }
2684
2685 # check for semaphores used as mutexes
2686 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2687 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2688 }
2689 # check for semaphores used as mutexes
2690 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2691 WARN("consider using a completion\n" . $herecurr);
2692
2693 }
2694 # recommend strict_strto* over simple_strto*
2695 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2696 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2697 }
2698 # check for __initcall(), use device_initcall() explicitly please
2699 if ($line =~ /^.\s*__initcall\s*\(/) {
2700 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2701 }
2702 # check for various ops structs, ensure they are const.
2703 my $struct_ops = qr{acpi_dock_ops|
2704 address_space_operations|
2705 backlight_ops|
2706 block_device_operations|
2707 dentry_operations|
2708 dev_pm_ops|
2709 dma_map_ops|
2710 extent_io_ops|
2711 file_lock_operations|
2712 file_operations|
2713 hv_ops|
2714 ide_dma_ops|
2715 intel_dvo_dev_ops|
2716 item_operations|
2717 iwl_ops|
2718 kgdb_arch|
2719 kgdb_io|
2720 kset_uevent_ops|
2721 lock_manager_operations|
2722 microcode_ops|
2723 mtrr_ops|
2724 neigh_ops|
2725 nlmsvc_binding|
2726 pci_raw_ops|
2727 pipe_buf_operations|
2728 platform_hibernation_ops|
2729 platform_suspend_ops|
2730 proto_ops|
2731 rpc_pipe_ops|
2732 seq_operations|
2733 snd_ac97_build_ops|
2734 soc_pcmcia_socket_ops|
2735 stacktrace_ops|
2736 sysfs_ops|
2737 tty_operations|
2738 usb_mon_operations|
2739 wd_ops}x;
2740 if ($line !~ /\bconst\b/ &&
2741 $line =~ /\bstruct\s+($struct_ops)\b/) {
2742 WARN("struct $1 should normally be const\n" .
2743 $herecurr);
2744 }
2745
2746 # use of NR_CPUS is usually wrong
2747 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2748 if ($line =~ /\bNR_CPUS\b/ &&
2749 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2750 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2751 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2752 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2753 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2754 {
2755 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2756 }
2757
2758 # check for %L{u,d,i} in strings
2759 my $string;
2760 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2761 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2762 $string =~ s/%%/__/g;
2763 if ($string =~ /(?<!%)%L[udi]/) {
2764 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2765 last;
2766 }
2767 }
2768
2769 # whine mightly about in_atomic
2770 if ($line =~ /\bin_atomic\s*\(/) {
2771 if ($realfile =~ m@^drivers/@) {
2772 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2773 } elsif ($realfile !~ m@^kernel/@) {
2774 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2775 }
2776 }
2777
2778 # check for lockdep_set_novalidate_class
2779 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2780 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2781 if ($realfile !~ m@^kernel/lockdep@ &&
2782 $realfile !~ m@^include/linux/lockdep@ &&
2783 $realfile !~ m@^drivers/base/core@) {
2784 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2785 }
2786 }
2787 }
2788
2789 # If we have no input at all, then there is nothing to report on
2790 # so just keep quiet.
2791 if ($#rawlines == -1) {
2792 exit(0);
2793 }
2794
2795 # In mailback mode only produce a report in the negative, for
2796 # things that appear to be patches.
2797 if ($mailback && ($clean == 1 || !$is_patch)) {
2798 exit(0);
2799 }
2800
2801 # This is not a patch, and we are are in 'no-patch' mode so
2802 # just keep quiet.
2803 if (!$chk_patch && !$is_patch) {
2804 exit(0);
2805 }
2806
2807 if (!$is_patch) {
2808 ERROR("Does not appear to be a unified-diff format patch\n");
2809 }
2810 if ($is_patch && $chk_signoff && $signoff == 0) {
2811 ERROR("Missing Signed-off-by: line(s)\n");
2812 }
2813
2814 print report_dump();
2815 if ($summary && !($clean == 1 && $quiet == 1)) {
2816 print "$filename " if ($summary_file);
2817 print "total: $cnt_error errors, $cnt_warn warnings, " .
2818 (($check)? "$cnt_chk checks, " : "") .
2819 "$cnt_lines lines checked\n";
2820 print "\n" if ($quiet == 0);
2821 }
2822
2823 if ($clean == 1 && $quiet == 0) {
2824 print "$vname has no obvious style problems and is ready for submission.\n"
2825 }
2826 if ($clean == 0 && $quiet == 0) {
2827 print "$vname has style problems, please review. If any of these errors\n";
2828 print "are false positives report them to the maintainer, see\n";
2829 print "CHECKPATCH in MAINTAINERS.\n";
2830 }
2831
2832 return $clean;
2833 }
This page took 0.146264 seconds and 6 git commands to generate.