scripts/checkpatch.pl: Remove check for deprecated features
[openwrt/staging/wigyori.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-2010 Andy Whitcroft <apw@canonical.com>
6 # (c) 2013 Vasilis Tsiligiannis <acinonyx@openwrt.gr> (adapt for OpenWrt tree)
7 # Licensed under the terms of the GNU GPL License version 2
8
9 use strict;
10
11 my $P = $0;
12 $P =~ s@.*/@@g;
13
14 my $V = '0.32-openwrt';
15
16 use Getopt::Long qw(:config no_auto_abbrev);
17
18 my $quiet = 0;
19 my $tree = 1;
20 my $chk_signoff = 1;
21 my $chk_patch = 1;
22 my $tst_only;
23 my $emacs = 0;
24 my $terse = 0;
25 my $file = 0;
26 my $check = 0;
27 my $summary = 1;
28 my $mailback = 0;
29 my $summary_file = 0;
30 my $show_types = 0;
31 my $root;
32 my %debug;
33 my %ignore_type = ();
34 my @ignore = ();
35 my $help = 0;
36 my $configuration_file = ".checkpatch.conf";
37
38 sub help {
39 my ($exitcode) = @_;
40
41 print << "EOM";
42 Usage: $P [OPTION]... [FILE]...
43 Version: $V
44
45 Options:
46 -q, --quiet quiet
47 --no-tree run without a kernel tree
48 --no-signoff do not check for 'Signed-off-by' line
49 --patch treat FILE as patchfile (default)
50 --emacs emacs compile window format
51 --terse one line per report
52 -f, --file treat FILE as regular source file
53 --subjective, --strict enable more subjective tests
54 --ignore TYPE(,TYPE2...) ignore various comma separated message types
55 --show-types show the message "types" in the output
56 --root=PATH PATH to the kernel tree root
57 --no-summary suppress the per-file summary
58 --mailback only produce a report in case of warnings/errors
59 --summary-file include the filename in summary
60 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
61 'values', 'possible', 'type', and 'attr' (default
62 is all off)
63 --test-only=WORD report only warnings/errors containing WORD
64 literally
65 -h, --help, --version display this help and exit
66
67 When FILE is - read standard input.
68 EOM
69
70 exit($exitcode);
71 }
72
73 my $conf = which_conf($configuration_file);
74 if (-f $conf) {
75 my @conf_args;
76 open(my $conffile, '<', "$conf")
77 or warn "$P: Can't find a readable $configuration_file file $!\n";
78
79 while (<$conffile>) {
80 my $line = $_;
81
82 $line =~ s/\s*\n?$//g;
83 $line =~ s/^\s*//g;
84 $line =~ s/\s+/ /g;
85
86 next if ($line =~ m/^\s*#/);
87 next if ($line =~ m/^\s*$/);
88
89 my @words = split(" ", $line);
90 foreach my $word (@words) {
91 last if ($word =~ m/^#/);
92 push (@conf_args, $word);
93 }
94 }
95 close($conffile);
96 unshift(@ARGV, @conf_args) if @conf_args;
97 }
98
99 GetOptions(
100 'q|quiet+' => \$quiet,
101 'tree!' => \$tree,
102 'signoff!' => \$chk_signoff,
103 'patch!' => \$chk_patch,
104 'emacs!' => \$emacs,
105 'terse!' => \$terse,
106 'f|file!' => \$file,
107 'subjective!' => \$check,
108 'strict!' => \$check,
109 'ignore=s' => \@ignore,
110 'show-types!' => \$show_types,
111 'root=s' => \$root,
112 'summary!' => \$summary,
113 'mailback!' => \$mailback,
114 'summary-file!' => \$summary_file,
115
116 'debug=s' => \%debug,
117 'test-only=s' => \$tst_only,
118 'h|help' => \$help,
119 'version' => \$help
120 ) or help(1);
121
122 help(0) if ($help);
123
124 my $exit = 0;
125
126 if ($#ARGV < 0) {
127 print "$P: no input files\n";
128 exit(1);
129 }
130
131 @ignore = split(/,/, join(',',@ignore));
132 foreach my $word (@ignore) {
133 $word =~ s/\s*\n?$//g;
134 $word =~ s/^\s*//g;
135 $word =~ s/\s+/ /g;
136 $word =~ tr/[a-z]/[A-Z]/;
137
138 next if ($word =~ m/^\s*#/);
139 next if ($word =~ m/^\s*$/);
140
141 $ignore_type{$word}++;
142 }
143
144 my $dbg_values = 0;
145 my $dbg_possible = 0;
146 my $dbg_type = 0;
147 my $dbg_attr = 0;
148 for my $key (keys %debug) {
149 ## no critic
150 eval "\${dbg_$key} = '$debug{$key}';";
151 die "$@" if ($@);
152 }
153
154 my $rpt_cleaners = 0;
155
156 if ($terse) {
157 $emacs = 1;
158 $quiet++;
159 }
160
161 if ($tree) {
162 if (defined $root) {
163 if (!top_of_openwrt_tree($root)) {
164 die "$P: $root: --root does not point at a valid tree\n";
165 }
166 } else {
167 if (top_of_openwrt_tree('.')) {
168 $root = '.';
169 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
170 top_of_openwrt_tree($1)) {
171 $root = $1;
172 }
173 }
174
175 if (!defined $root) {
176 print "Must be run from the top-level dir. of a OpenWrt tree\n";
177 exit(2);
178 }
179 }
180
181 my $emitted_corrupt = 0;
182
183 our $Ident = qr{
184 [A-Za-z_][A-Za-z\d_]*
185 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
186 }x;
187 our $Storage = qr{extern|static|asmlinkage};
188 our $Sparse = qr{
189 __user|
190 __kernel|
191 __force|
192 __iomem|
193 __must_check|
194 __init_refok|
195 __kprobes|
196 __ref|
197 __rcu
198 }x;
199
200 # Notes to $Attribute:
201 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
202 our $Attribute = qr{
203 const|
204 __percpu|
205 __nocast|
206 __safe|
207 __bitwise__|
208 __packed__|
209 __packed2__|
210 __naked|
211 __maybe_unused|
212 __always_unused|
213 __noreturn|
214 __used|
215 __cold|
216 __noclone|
217 __deprecated|
218 __read_mostly|
219 __kprobes|
220 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
221 ____cacheline_aligned|
222 ____cacheline_aligned_in_smp|
223 ____cacheline_internodealigned_in_smp|
224 __weak
225 }x;
226 our $Modifier;
227 our $Inline = qr{inline|__always_inline|noinline};
228 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
229 our $Lval = qr{$Ident(?:$Member)*};
230
231 our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
232 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
233 our $Compare = qr{<=|>=|==|!=|<|>};
234 our $Operators = qr{
235 <=|>=|==|!=|
236 =>|->|<<|>>|<|>|!|~|
237 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
238 }x;
239
240 our $NonptrType;
241 our $Type;
242 our $Declare;
243
244 our $NON_ASCII_UTF8 = qr{
245 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
246 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
247 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
248 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
249 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
250 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
251 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
252 }x;
253
254 our $UTF8 = qr{
255 [\x09\x0A\x0D\x20-\x7E] # ASCII
256 | $NON_ASCII_UTF8
257 }x;
258
259 our $typeTypedefs = qr{(?x:
260 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
261 atomic_t
262 )};
263
264 our $logFunctions = qr{(?x:
265 printk(?:_ratelimited|_once|)|
266 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
267 WARN(?:_RATELIMIT|_ONCE|)|
268 panic|
269 MODULE_[A-Z_]+
270 )};
271
272 our $signature_tags = qr{(?xi:
273 Signed-off-by:|
274 Acked-by:|
275 Tested-by:|
276 Reviewed-by:|
277 Reported-by:|
278 To:|
279 Cc:
280 )};
281
282 our @typeList = (
283 qr{void},
284 qr{(?:unsigned\s+)?char},
285 qr{(?:unsigned\s+)?short},
286 qr{(?:unsigned\s+)?int},
287 qr{(?:unsigned\s+)?long},
288 qr{(?:unsigned\s+)?long\s+int},
289 qr{(?:unsigned\s+)?long\s+long},
290 qr{(?:unsigned\s+)?long\s+long\s+int},
291 qr{unsigned},
292 qr{float},
293 qr{double},
294 qr{bool},
295 qr{struct\s+$Ident},
296 qr{union\s+$Ident},
297 qr{enum\s+$Ident},
298 qr{${Ident}_t},
299 qr{${Ident}_handler},
300 qr{${Ident}_handler_fn},
301 );
302 our @modifierList = (
303 qr{fastcall},
304 );
305
306 our $allowed_asm_includes = qr{(?x:
307 irq|
308 memory
309 )};
310 # memory.h: ARM has a custom one
311
312 sub build_types {
313 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
314 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
315 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
316 $NonptrType = qr{
317 (?:$Modifier\s+|const\s+)*
318 (?:
319 (?:typeof|__typeof__)\s*\([^\)]*\)|
320 (?:$typeTypedefs\b)|
321 (?:${all}\b)
322 )
323 (?:\s+$Modifier|\s+const)*
324 }x;
325 $Type = qr{
326 $NonptrType
327 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
328 (?:\s+$Inline|\s+$Modifier)*
329 }x;
330 $Declare = qr{(?:$Storage\s+)?$Type};
331 }
332 build_types();
333
334
335 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
336
337 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
338 # requires at least perl version v5.10.0
339 # Any use must be runtime checked with $^V
340
341 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
342 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
343 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
344
345 sub deparenthesize {
346 my ($string) = @_;
347 return "" if (!defined($string));
348 $string =~ s@^\s*\(\s*@@g;
349 $string =~ s@\s*\)\s*$@@g;
350 $string =~ s@\s+@ @g;
351 return $string;
352 }
353
354 $chk_signoff = 0 if ($file);
355
356 my @rawlines = ();
357 my @lines = ();
358 my $vname;
359 for my $filename (@ARGV) {
360 my $FILE;
361 if ($file) {
362 open($FILE, '-|', "diff -u /dev/null $filename") ||
363 die "$P: $filename: diff failed - $!\n";
364 } elsif ($filename eq '-') {
365 open($FILE, '<&STDIN');
366 } else {
367 open($FILE, '<', "$filename") ||
368 die "$P: $filename: open failed - $!\n";
369 }
370 if ($filename eq '-') {
371 $vname = 'Your patch';
372 } else {
373 $vname = $filename;
374 }
375 while (<$FILE>) {
376 chomp;
377 push(@rawlines, $_);
378 }
379 close($FILE);
380 if (!process($filename)) {
381 $exit = 1;
382 }
383 @rawlines = ();
384 @lines = ();
385 }
386
387 exit($exit);
388
389 sub top_of_openwrt_tree {
390 my ($root) = @_;
391
392 my @tree_check = (
393 "BSDmakefile", "Config.in", "LICENSE", "Makefile", "README",
394 "docs", "feeds.conf.default", "include", "package", "rules.mk",
395 "scripts", "target", "toolchain", "tools"
396 );
397
398 foreach my $check (@tree_check) {
399 if (! -e $root . '/' . $check) {
400 return 0;
401 }
402 }
403 return 1;
404 }
405
406 sub parse_email {
407 my ($formatted_email) = @_;
408
409 my $name = "";
410 my $address = "";
411 my $comment = "";
412
413 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
414 $name = $1;
415 $address = $2;
416 $comment = $3 if defined $3;
417 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
418 $address = $1;
419 $comment = $2 if defined $2;
420 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
421 $address = $1;
422 $comment = $2 if defined $2;
423 $formatted_email =~ s/$address.*$//;
424 $name = $formatted_email;
425 $name =~ s/^\s+|\s+$//g;
426 $name =~ s/^\"|\"$//g;
427 # If there's a name left after stripping spaces and
428 # leading quotes, and the address doesn't have both
429 # leading and trailing angle brackets, the address
430 # is invalid. ie:
431 # "joe smith joe@smith.com" bad
432 # "joe smith <joe@smith.com" bad
433 if ($name ne "" && $address !~ /^<[^>]+>$/) {
434 $name = "";
435 $address = "";
436 $comment = "";
437 }
438 }
439
440 $name =~ s/^\s+|\s+$//g;
441 $name =~ s/^\"|\"$//g;
442 $address =~ s/^\s+|\s+$//g;
443 $address =~ s/^\<|\>$//g;
444
445 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
446 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
447 $name = "\"$name\"";
448 }
449
450 return ($name, $address, $comment);
451 }
452
453 sub format_email {
454 my ($name, $address) = @_;
455
456 my $formatted_email;
457
458 $name =~ s/^\s+|\s+$//g;
459 $name =~ s/^\"|\"$//g;
460 $address =~ s/^\s+|\s+$//g;
461
462 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
463 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
464 $name = "\"$name\"";
465 }
466
467 if ("$name" eq "") {
468 $formatted_email = "$address";
469 } else {
470 $formatted_email = "$name <$address>";
471 }
472
473 return $formatted_email;
474 }
475
476 sub which_conf {
477 my ($conf) = @_;
478
479 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
480 if (-e "$path/$conf") {
481 return "$path/$conf";
482 }
483 }
484
485 return "";
486 }
487
488 sub expand_tabs {
489 my ($str) = @_;
490
491 my $res = '';
492 my $n = 0;
493 for my $c (split(//, $str)) {
494 if ($c eq "\t") {
495 $res .= ' ';
496 $n++;
497 for (; ($n % 8) != 0; $n++) {
498 $res .= ' ';
499 }
500 next;
501 }
502 $res .= $c;
503 $n++;
504 }
505
506 return $res;
507 }
508 sub copy_spacing {
509 (my $res = shift) =~ tr/\t/ /c;
510 return $res;
511 }
512
513 sub line_stats {
514 my ($line) = @_;
515
516 # Drop the diff line leader and expand tabs
517 $line =~ s/^.//;
518 $line = expand_tabs($line);
519
520 # Pick the indent from the front of the line.
521 my ($white) = ($line =~ /^(\s*)/);
522
523 return (length($line), length($white));
524 }
525
526 my $sanitise_quote = '';
527
528 sub sanitise_line_reset {
529 my ($in_comment) = @_;
530
531 if ($in_comment) {
532 $sanitise_quote = '*/';
533 } else {
534 $sanitise_quote = '';
535 }
536 }
537 sub sanitise_line {
538 my ($line) = @_;
539
540 my $res = '';
541 my $l = '';
542
543 my $qlen = 0;
544 my $off = 0;
545 my $c;
546
547 # Always copy over the diff marker.
548 $res = substr($line, 0, 1);
549
550 for ($off = 1; $off < length($line); $off++) {
551 $c = substr($line, $off, 1);
552
553 # Comments we are wacking completly including the begin
554 # and end, all to $;.
555 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
556 $sanitise_quote = '*/';
557
558 substr($res, $off, 2, "$;$;");
559 $off++;
560 next;
561 }
562 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
563 $sanitise_quote = '';
564 substr($res, $off, 2, "$;$;");
565 $off++;
566 next;
567 }
568 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
569 $sanitise_quote = '//';
570
571 substr($res, $off, 2, $sanitise_quote);
572 $off++;
573 next;
574 }
575
576 # A \ in a string means ignore the next character.
577 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
578 $c eq "\\") {
579 substr($res, $off, 2, 'XX');
580 $off++;
581 next;
582 }
583 # Regular quotes.
584 if ($c eq "'" || $c eq '"') {
585 if ($sanitise_quote eq '') {
586 $sanitise_quote = $c;
587
588 substr($res, $off, 1, $c);
589 next;
590 } elsif ($sanitise_quote eq $c) {
591 $sanitise_quote = '';
592 }
593 }
594
595 #print "c<$c> SQ<$sanitise_quote>\n";
596 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
597 substr($res, $off, 1, $;);
598 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
599 substr($res, $off, 1, $;);
600 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
601 substr($res, $off, 1, 'X');
602 } else {
603 substr($res, $off, 1, $c);
604 }
605 }
606
607 if ($sanitise_quote eq '//') {
608 $sanitise_quote = '';
609 }
610
611 # The pathname on a #include may be surrounded by '<' and '>'.
612 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
613 my $clean = 'X' x length($1);
614 $res =~ s@\<.*\>@<$clean>@;
615
616 # The whole of a #error is a string.
617 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
618 my $clean = 'X' x length($1);
619 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
620 }
621
622 return $res;
623 }
624
625 sub ctx_statement_block {
626 my ($linenr, $remain, $off) = @_;
627 my $line = $linenr - 1;
628 my $blk = '';
629 my $soff = $off;
630 my $coff = $off - 1;
631 my $coff_set = 0;
632
633 my $loff = 0;
634
635 my $type = '';
636 my $level = 0;
637 my @stack = ();
638 my $p;
639 my $c;
640 my $len = 0;
641
642 my $remainder;
643 while (1) {
644 @stack = (['', 0]) if ($#stack == -1);
645
646 #warn "CSB: blk<$blk> remain<$remain>\n";
647 # If we are about to drop off the end, pull in more
648 # context.
649 if ($off >= $len) {
650 for (; $remain > 0; $line++) {
651 last if (!defined $lines[$line]);
652 next if ($lines[$line] =~ /^-/);
653 $remain--;
654 $loff = $len;
655 $blk .= $lines[$line] . "\n";
656 $len = length($blk);
657 $line++;
658 last;
659 }
660 # Bail if there is no further context.
661 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
662 if ($off >= $len) {
663 last;
664 }
665 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
666 $level++;
667 $type = '#';
668 }
669 }
670 $p = $c;
671 $c = substr($blk, $off, 1);
672 $remainder = substr($blk, $off);
673
674 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
675
676 # Handle nested #if/#else.
677 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
678 push(@stack, [ $type, $level ]);
679 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
680 ($type, $level) = @{$stack[$#stack - 1]};
681 } elsif ($remainder =~ /^#\s*endif\b/) {
682 ($type, $level) = @{pop(@stack)};
683 }
684
685 # Statement ends at the ';' or a close '}' at the
686 # outermost level.
687 if ($level == 0 && $c eq ';') {
688 last;
689 }
690
691 # An else is really a conditional as long as its not else if
692 if ($level == 0 && $coff_set == 0 &&
693 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
694 $remainder =~ /^(else)(?:\s|{)/ &&
695 $remainder !~ /^else\s+if\b/) {
696 $coff = $off + length($1) - 1;
697 $coff_set = 1;
698 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
699 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
700 }
701
702 if (($type eq '' || $type eq '(') && $c eq '(') {
703 $level++;
704 $type = '(';
705 }
706 if ($type eq '(' && $c eq ')') {
707 $level--;
708 $type = ($level != 0)? '(' : '';
709
710 if ($level == 0 && $coff < $soff) {
711 $coff = $off;
712 $coff_set = 1;
713 #warn "CSB: mark coff<$coff>\n";
714 }
715 }
716 if (($type eq '' || $type eq '{') && $c eq '{') {
717 $level++;
718 $type = '{';
719 }
720 if ($type eq '{' && $c eq '}') {
721 $level--;
722 $type = ($level != 0)? '{' : '';
723
724 if ($level == 0) {
725 if (substr($blk, $off + 1, 1) eq ';') {
726 $off++;
727 }
728 last;
729 }
730 }
731 # Preprocessor commands end at the newline unless escaped.
732 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
733 $level--;
734 $type = '';
735 $off++;
736 last;
737 }
738 $off++;
739 }
740 # We are truly at the end, so shuffle to the next line.
741 if ($off == $len) {
742 $loff = $len + 1;
743 $line++;
744 $remain--;
745 }
746
747 my $statement = substr($blk, $soff, $off - $soff + 1);
748 my $condition = substr($blk, $soff, $coff - $soff + 1);
749
750 #warn "STATEMENT<$statement>\n";
751 #warn "CONDITION<$condition>\n";
752
753 #print "coff<$coff> soff<$off> loff<$loff>\n";
754
755 return ($statement, $condition,
756 $line, $remain + 1, $off - $loff + 1, $level);
757 }
758
759 sub statement_lines {
760 my ($stmt) = @_;
761
762 # Strip the diff line prefixes and rip blank lines at start and end.
763 $stmt =~ s/(^|\n)./$1/g;
764 $stmt =~ s/^\s*//;
765 $stmt =~ s/\s*$//;
766
767 my @stmt_lines = ($stmt =~ /\n/g);
768
769 return $#stmt_lines + 2;
770 }
771
772 sub statement_rawlines {
773 my ($stmt) = @_;
774
775 my @stmt_lines = ($stmt =~ /\n/g);
776
777 return $#stmt_lines + 2;
778 }
779
780 sub statement_block_size {
781 my ($stmt) = @_;
782
783 $stmt =~ s/(^|\n)./$1/g;
784 $stmt =~ s/^\s*{//;
785 $stmt =~ s/}\s*$//;
786 $stmt =~ s/^\s*//;
787 $stmt =~ s/\s*$//;
788
789 my @stmt_lines = ($stmt =~ /\n/g);
790 my @stmt_statements = ($stmt =~ /;/g);
791
792 my $stmt_lines = $#stmt_lines + 2;
793 my $stmt_statements = $#stmt_statements + 1;
794
795 if ($stmt_lines > $stmt_statements) {
796 return $stmt_lines;
797 } else {
798 return $stmt_statements;
799 }
800 }
801
802 sub ctx_statement_full {
803 my ($linenr, $remain, $off) = @_;
804 my ($statement, $condition, $level);
805
806 my (@chunks);
807
808 # Grab the first conditional/block pair.
809 ($statement, $condition, $linenr, $remain, $off, $level) =
810 ctx_statement_block($linenr, $remain, $off);
811 #print "F: c<$condition> s<$statement> remain<$remain>\n";
812 push(@chunks, [ $condition, $statement ]);
813 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
814 return ($level, $linenr, @chunks);
815 }
816
817 # Pull in the following conditional/block pairs and see if they
818 # could continue the statement.
819 for (;;) {
820 ($statement, $condition, $linenr, $remain, $off, $level) =
821 ctx_statement_block($linenr, $remain, $off);
822 #print "C: c<$condition> s<$statement> remain<$remain>\n";
823 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
824 #print "C: push\n";
825 push(@chunks, [ $condition, $statement ]);
826 }
827
828 return ($level, $linenr, @chunks);
829 }
830
831 sub ctx_block_get {
832 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
833 my $line;
834 my $start = $linenr - 1;
835 my $blk = '';
836 my @o;
837 my @c;
838 my @res = ();
839
840 my $level = 0;
841 my @stack = ($level);
842 for ($line = $start; $remain > 0; $line++) {
843 next if ($rawlines[$line] =~ /^-/);
844 $remain--;
845
846 $blk .= $rawlines[$line];
847
848 # Handle nested #if/#else.
849 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
850 push(@stack, $level);
851 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
852 $level = $stack[$#stack - 1];
853 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
854 $level = pop(@stack);
855 }
856
857 foreach my $c (split(//, $lines[$line])) {
858 ##print "C<$c>L<$level><$open$close>O<$off>\n";
859 if ($off > 0) {
860 $off--;
861 next;
862 }
863
864 if ($c eq $close && $level > 0) {
865 $level--;
866 last if ($level == 0);
867 } elsif ($c eq $open) {
868 $level++;
869 }
870 }
871
872 if (!$outer || $level <= 1) {
873 push(@res, $rawlines[$line]);
874 }
875
876 last if ($level == 0);
877 }
878
879 return ($level, @res);
880 }
881 sub ctx_block_outer {
882 my ($linenr, $remain) = @_;
883
884 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
885 return @r;
886 }
887 sub ctx_block {
888 my ($linenr, $remain) = @_;
889
890 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
891 return @r;
892 }
893 sub ctx_statement {
894 my ($linenr, $remain, $off) = @_;
895
896 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
897 return @r;
898 }
899 sub ctx_block_level {
900 my ($linenr, $remain) = @_;
901
902 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
903 }
904 sub ctx_statement_level {
905 my ($linenr, $remain, $off) = @_;
906
907 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
908 }
909
910 sub ctx_locate_comment {
911 my ($first_line, $end_line) = @_;
912
913 # Catch a comment on the end of the line itself.
914 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
915 return $current_comment if (defined $current_comment);
916
917 # Look through the context and try and figure out if there is a
918 # comment.
919 my $in_comment = 0;
920 $current_comment = '';
921 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
922 my $line = $rawlines[$linenr - 1];
923 #warn " $line\n";
924 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
925 $in_comment = 1;
926 }
927 if ($line =~ m@/\*@) {
928 $in_comment = 1;
929 }
930 if (!$in_comment && $current_comment ne '') {
931 $current_comment = '';
932 }
933 $current_comment .= $line . "\n" if ($in_comment);
934 if ($line =~ m@\*/@) {
935 $in_comment = 0;
936 }
937 }
938
939 chomp($current_comment);
940 return($current_comment);
941 }
942 sub ctx_has_comment {
943 my ($first_line, $end_line) = @_;
944 my $cmt = ctx_locate_comment($first_line, $end_line);
945
946 ##print "LINE: $rawlines[$end_line - 1 ]\n";
947 ##print "CMMT: $cmt\n";
948
949 return ($cmt ne '');
950 }
951
952 sub raw_line {
953 my ($linenr, $cnt) = @_;
954
955 my $offset = $linenr - 1;
956 $cnt++;
957
958 my $line;
959 while ($cnt) {
960 $line = $rawlines[$offset++];
961 next if (defined($line) && $line =~ /^-/);
962 $cnt--;
963 }
964
965 return $line;
966 }
967
968 sub cat_vet {
969 my ($vet) = @_;
970 my ($res, $coded);
971
972 $res = '';
973 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
974 $res .= $1;
975 if ($2 ne '') {
976 $coded = sprintf("^%c", unpack('C', $2) + 64);
977 $res .= $coded;
978 }
979 }
980 $res =~ s/$/\$/;
981
982 return $res;
983 }
984
985 my $av_preprocessor = 0;
986 my $av_pending;
987 my @av_paren_type;
988 my $av_pend_colon;
989
990 sub annotate_reset {
991 $av_preprocessor = 0;
992 $av_pending = '_';
993 @av_paren_type = ('E');
994 $av_pend_colon = 'O';
995 }
996
997 sub annotate_values {
998 my ($stream, $type) = @_;
999
1000 my $res;
1001 my $var = '_' x length($stream);
1002 my $cur = $stream;
1003
1004 print "$stream\n" if ($dbg_values > 1);
1005
1006 while (length($cur)) {
1007 @av_paren_type = ('E') if ($#av_paren_type < 0);
1008 print " <" . join('', @av_paren_type) .
1009 "> <$type> <$av_pending>" if ($dbg_values > 1);
1010 if ($cur =~ /^(\s+)/o) {
1011 print "WS($1)\n" if ($dbg_values > 1);
1012 if ($1 =~ /\n/ && $av_preprocessor) {
1013 $type = pop(@av_paren_type);
1014 $av_preprocessor = 0;
1015 }
1016
1017 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1018 print "CAST($1)\n" if ($dbg_values > 1);
1019 push(@av_paren_type, $type);
1020 $type = 'c';
1021
1022 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1023 print "DECLARE($1)\n" if ($dbg_values > 1);
1024 $type = 'T';
1025
1026 } elsif ($cur =~ /^($Modifier)\s*/) {
1027 print "MODIFIER($1)\n" if ($dbg_values > 1);
1028 $type = 'T';
1029
1030 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1031 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1032 $av_preprocessor = 1;
1033 push(@av_paren_type, $type);
1034 if ($2 ne '') {
1035 $av_pending = 'N';
1036 }
1037 $type = 'E';
1038
1039 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1040 print "UNDEF($1)\n" if ($dbg_values > 1);
1041 $av_preprocessor = 1;
1042 push(@av_paren_type, $type);
1043
1044 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1045 print "PRE_START($1)\n" if ($dbg_values > 1);
1046 $av_preprocessor = 1;
1047
1048 push(@av_paren_type, $type);
1049 push(@av_paren_type, $type);
1050 $type = 'E';
1051
1052 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1053 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1054 $av_preprocessor = 1;
1055
1056 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1057
1058 $type = 'E';
1059
1060 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1061 print "PRE_END($1)\n" if ($dbg_values > 1);
1062
1063 $av_preprocessor = 1;
1064
1065 # Assume all arms of the conditional end as this
1066 # one does, and continue as if the #endif was not here.
1067 pop(@av_paren_type);
1068 push(@av_paren_type, $type);
1069 $type = 'E';
1070
1071 } elsif ($cur =~ /^(\\\n)/o) {
1072 print "PRECONT($1)\n" if ($dbg_values > 1);
1073
1074 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1075 print "ATTR($1)\n" if ($dbg_values > 1);
1076 $av_pending = $type;
1077 $type = 'N';
1078
1079 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1080 print "SIZEOF($1)\n" if ($dbg_values > 1);
1081 if (defined $2) {
1082 $av_pending = 'V';
1083 }
1084 $type = 'N';
1085
1086 } elsif ($cur =~ /^(if|while|for)\b/o) {
1087 print "COND($1)\n" if ($dbg_values > 1);
1088 $av_pending = 'E';
1089 $type = 'N';
1090
1091 } elsif ($cur =~/^(case)/o) {
1092 print "CASE($1)\n" if ($dbg_values > 1);
1093 $av_pend_colon = 'C';
1094 $type = 'N';
1095
1096 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1097 print "KEYWORD($1)\n" if ($dbg_values > 1);
1098 $type = 'N';
1099
1100 } elsif ($cur =~ /^(\()/o) {
1101 print "PAREN('$1')\n" if ($dbg_values > 1);
1102 push(@av_paren_type, $av_pending);
1103 $av_pending = '_';
1104 $type = 'N';
1105
1106 } elsif ($cur =~ /^(\))/o) {
1107 my $new_type = pop(@av_paren_type);
1108 if ($new_type ne '_') {
1109 $type = $new_type;
1110 print "PAREN('$1') -> $type\n"
1111 if ($dbg_values > 1);
1112 } else {
1113 print "PAREN('$1')\n" if ($dbg_values > 1);
1114 }
1115
1116 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1117 print "FUNC($1)\n" if ($dbg_values > 1);
1118 $type = 'V';
1119 $av_pending = 'V';
1120
1121 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1122 if (defined $2 && $type eq 'C' || $type eq 'T') {
1123 $av_pend_colon = 'B';
1124 } elsif ($type eq 'E') {
1125 $av_pend_colon = 'L';
1126 }
1127 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1128 $type = 'V';
1129
1130 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1131 print "IDENT($1)\n" if ($dbg_values > 1);
1132 $type = 'V';
1133
1134 } elsif ($cur =~ /^($Assignment)/o) {
1135 print "ASSIGN($1)\n" if ($dbg_values > 1);
1136 $type = 'N';
1137
1138 } elsif ($cur =~/^(;|{|})/) {
1139 print "END($1)\n" if ($dbg_values > 1);
1140 $type = 'E';
1141 $av_pend_colon = 'O';
1142
1143 } elsif ($cur =~/^(,)/) {
1144 print "COMMA($1)\n" if ($dbg_values > 1);
1145 $type = 'C';
1146
1147 } elsif ($cur =~ /^(\?)/o) {
1148 print "QUESTION($1)\n" if ($dbg_values > 1);
1149 $type = 'N';
1150
1151 } elsif ($cur =~ /^(:)/o) {
1152 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1153
1154 substr($var, length($res), 1, $av_pend_colon);
1155 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1156 $type = 'E';
1157 } else {
1158 $type = 'N';
1159 }
1160 $av_pend_colon = 'O';
1161
1162 } elsif ($cur =~ /^(\[)/o) {
1163 print "CLOSE($1)\n" if ($dbg_values > 1);
1164 $type = 'N';
1165
1166 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1167 my $variant;
1168
1169 print "OPV($1)\n" if ($dbg_values > 1);
1170 if ($type eq 'V') {
1171 $variant = 'B';
1172 } else {
1173 $variant = 'U';
1174 }
1175
1176 substr($var, length($res), 1, $variant);
1177 $type = 'N';
1178
1179 } elsif ($cur =~ /^($Operators)/o) {
1180 print "OP($1)\n" if ($dbg_values > 1);
1181 if ($1 ne '++' && $1 ne '--') {
1182 $type = 'N';
1183 }
1184
1185 } elsif ($cur =~ /(^.)/o) {
1186 print "C($1)\n" if ($dbg_values > 1);
1187 }
1188 if (defined $1) {
1189 $cur = substr($cur, length($1));
1190 $res .= $type x length($1);
1191 }
1192 }
1193
1194 return ($res, $var);
1195 }
1196
1197 sub possible {
1198 my ($possible, $line) = @_;
1199 my $notPermitted = qr{(?:
1200 ^(?:
1201 $Modifier|
1202 $Storage|
1203 $Type|
1204 DEFINE_\S+
1205 )$|
1206 ^(?:
1207 goto|
1208 return|
1209 case|
1210 else|
1211 asm|__asm__|
1212 do|
1213 \#|
1214 \#\#|
1215 )(?:\s|$)|
1216 ^(?:typedef|struct|enum)\b
1217 )}x;
1218 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1219 if ($possible !~ $notPermitted) {
1220 # Check for modifiers.
1221 $possible =~ s/\s*$Storage\s*//g;
1222 $possible =~ s/\s*$Sparse\s*//g;
1223 if ($possible =~ /^\s*$/) {
1224
1225 } elsif ($possible =~ /\s/) {
1226 $possible =~ s/\s*$Type\s*//g;
1227 for my $modifier (split(' ', $possible)) {
1228 if ($modifier !~ $notPermitted) {
1229 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1230 push(@modifierList, $modifier);
1231 }
1232 }
1233
1234 } else {
1235 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1236 push(@typeList, $possible);
1237 }
1238 build_types();
1239 } else {
1240 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1241 }
1242 }
1243
1244 my $prefix = '';
1245
1246 sub show_type {
1247 return !defined $ignore_type{$_[0]};
1248 }
1249
1250 sub report {
1251 if (!show_type($_[1]) ||
1252 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1253 return 0;
1254 }
1255 my $line;
1256 if ($show_types) {
1257 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1258 } else {
1259 $line = "$prefix$_[0]: $_[2]\n";
1260 }
1261 $line = (split('\n', $line))[0] . "\n" if ($terse);
1262
1263 push(our @report, $line);
1264
1265 return 1;
1266 }
1267 sub report_dump {
1268 our @report;
1269 }
1270
1271 sub ERROR {
1272 if (report("ERROR", $_[0], $_[1])) {
1273 our $clean = 0;
1274 our $cnt_error++;
1275 }
1276 }
1277 sub WARN {
1278 if (report("WARNING", $_[0], $_[1])) {
1279 our $clean = 0;
1280 our $cnt_warn++;
1281 }
1282 }
1283 sub CHK {
1284 if ($check && report("CHECK", $_[0], $_[1])) {
1285 our $clean = 0;
1286 our $cnt_chk++;
1287 }
1288 }
1289
1290 sub check_absolute_file {
1291 my ($absolute, $herecurr) = @_;
1292 my $file = $absolute;
1293
1294 ##print "absolute<$absolute>\n";
1295
1296 # See if any suffix of this path is a path within the tree.
1297 while ($file =~ s@^[^/]*/@@) {
1298 if (-f "$root/$file") {
1299 ##print "file<$file>\n";
1300 last;
1301 }
1302 }
1303 if (! -f _) {
1304 return 0;
1305 }
1306
1307 # It is, so see if the prefix is acceptable.
1308 my $prefix = $absolute;
1309 substr($prefix, -length($file)) = '';
1310
1311 ##print "prefix<$prefix>\n";
1312 if ($prefix ne ".../") {
1313 WARN("USE_RELATIVE_PATH",
1314 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1315 }
1316 }
1317
1318 sub pos_last_openparen {
1319 my ($line) = @_;
1320
1321 my $pos = 0;
1322
1323 my $opens = $line =~ tr/\(/\(/;
1324 my $closes = $line =~ tr/\)/\)/;
1325
1326 my $last_openparen = 0;
1327
1328 if (($opens == 0) || ($closes >= $opens)) {
1329 return -1;
1330 }
1331
1332 my $len = length($line);
1333
1334 for ($pos = 0; $pos < $len; $pos++) {
1335 my $string = substr($line, $pos);
1336 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1337 $pos += length($1) - 1;
1338 } elsif (substr($line, $pos, 1) eq '(') {
1339 $last_openparen = $pos;
1340 } elsif (index($string, '(') == -1) {
1341 last;
1342 }
1343 }
1344
1345 return $last_openparen + 1;
1346 }
1347
1348 sub process {
1349 my $filename = shift;
1350
1351 my $linenr=0;
1352 my $prevline="";
1353 my $prevrawline="";
1354 my $stashline="";
1355 my $stashrawline="";
1356
1357 my $length;
1358 my $indent;
1359 my $previndent=0;
1360 my $stashindent=0;
1361
1362 our $clean = 1;
1363 my $signoff = 0;
1364 my $is_patch = 0;
1365
1366 my $in_header_lines = 1;
1367 my $in_commit_log = 0; #Scanning lines before patch
1368
1369 our @report = ();
1370 our $cnt_lines = 0;
1371 our $cnt_error = 0;
1372 our $cnt_warn = 0;
1373 our $cnt_chk = 0;
1374
1375 # Trace the real file/line as we go.
1376 my $realfile = '';
1377 my $realline = 0;
1378 my $realcnt = 0;
1379 my $here = '';
1380 my $in_comment = 0;
1381 my $comment_edge = 0;
1382 my $first_line = 0;
1383 my $p1_prefix = '';
1384
1385 my $prev_values = 'E';
1386
1387 # suppression flags
1388 my %suppress_ifbraces;
1389 my %suppress_whiletrailers;
1390 my %suppress_export;
1391 my $suppress_statement = 0;
1392
1393 # Pre-scan the patch sanitizing the lines.
1394 # Pre-scan the patch looking for any __setup documentation.
1395 #
1396 my @setup_docs = ();
1397 my $setup_docs = 0;
1398
1399 sanitise_line_reset();
1400 my $line;
1401 foreach my $rawline (@rawlines) {
1402 $linenr++;
1403 $line = $rawline;
1404
1405 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1406 $setup_docs = 0;
1407 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1408 $setup_docs = 1;
1409 }
1410 #next;
1411 }
1412 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1413 $realline=$1-1;
1414 if (defined $2) {
1415 $realcnt=$3+1;
1416 } else {
1417 $realcnt=1+1;
1418 }
1419 $in_comment = 0;
1420
1421 # Guestimate if this is a continuing comment. Run
1422 # the context looking for a comment "edge". If this
1423 # edge is a close comment then we must be in a comment
1424 # at context start.
1425 my $edge;
1426 my $cnt = $realcnt;
1427 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1428 next if (defined $rawlines[$ln - 1] &&
1429 $rawlines[$ln - 1] =~ /^-/);
1430 $cnt--;
1431 #print "RAW<$rawlines[$ln - 1]>\n";
1432 last if (!defined $rawlines[$ln - 1]);
1433 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1434 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1435 ($edge) = $1;
1436 last;
1437 }
1438 }
1439 if (defined $edge && $edge eq '*/') {
1440 $in_comment = 1;
1441 }
1442
1443 # Guestimate if this is a continuing comment. If this
1444 # is the start of a diff block and this line starts
1445 # ' *' then it is very likely a comment.
1446 if (!defined $edge &&
1447 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1448 {
1449 $in_comment = 1;
1450 }
1451
1452 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1453 sanitise_line_reset($in_comment);
1454
1455 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1456 # Standardise the strings and chars within the input to
1457 # simplify matching -- only bother with positive lines.
1458 $line = sanitise_line($rawline);
1459 }
1460 push(@lines, $line);
1461
1462 if ($realcnt > 1) {
1463 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1464 } else {
1465 $realcnt = 0;
1466 }
1467
1468 #print "==>$rawline\n";
1469 #print "-->$line\n";
1470
1471 if ($setup_docs && $line =~ /^\+/) {
1472 push(@setup_docs, $line);
1473 }
1474 }
1475
1476 $prefix = '';
1477
1478 $realcnt = 0;
1479 $linenr = 0;
1480 foreach my $line (@lines) {
1481 $linenr++;
1482
1483 my $rawline = $rawlines[$linenr - 1];
1484
1485 #extract the line range in the file after the patch is applied
1486 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1487 $is_patch = 1;
1488 $first_line = $linenr + 1;
1489 $realline=$1-1;
1490 if (defined $2) {
1491 $realcnt=$3+1;
1492 } else {
1493 $realcnt=1+1;
1494 }
1495 annotate_reset();
1496 $prev_values = 'E';
1497
1498 %suppress_ifbraces = ();
1499 %suppress_whiletrailers = ();
1500 %suppress_export = ();
1501 $suppress_statement = 0;
1502 next;
1503
1504 # track the line number as we move through the hunk, note that
1505 # new versions of GNU diff omit the leading space on completely
1506 # blank context lines so we need to count that too.
1507 } elsif ($line =~ /^( |\+|$)/) {
1508 $realline++;
1509 $realcnt-- if ($realcnt != 0);
1510
1511 # Measure the line length and indent.
1512 ($length, $indent) = line_stats($rawline);
1513
1514 # Track the previous line.
1515 ($prevline, $stashline) = ($stashline, $line);
1516 ($previndent, $stashindent) = ($stashindent, $indent);
1517 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1518
1519 #warn "line<$line>\n";
1520
1521 } elsif ($realcnt == 1) {
1522 $realcnt--;
1523 }
1524
1525 my $hunk_line = ($realcnt != 0);
1526
1527 #make up the handle for any error we report on this line
1528 $prefix = "$filename:$realline: " if ($emacs && $file);
1529 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1530
1531 $here = "#$linenr: " if (!$file);
1532 $here = "#$realline: " if ($file);
1533
1534 # extract the filename as it passes
1535 if ($line =~ /^diff --git.*?(\S+)$/) {
1536 $realfile = $1;
1537 $realfile =~ s@^([^/]*)/@@;
1538 $in_commit_log = 0;
1539 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1540 $realfile = $1;
1541 $realfile =~ s@^([^/]*)/@@;
1542 $in_commit_log = 0;
1543
1544 $p1_prefix = $1;
1545 if (!$file && $tree && $p1_prefix ne '' &&
1546 -e "$root/$p1_prefix") {
1547 WARN("PATCH_PREFIX",
1548 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1549 }
1550
1551 if ($realfile =~ m@^include/asm/@) {
1552 ERROR("MODIFIED_INCLUDE_ASM",
1553 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1554 }
1555 next;
1556 }
1557
1558 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1559
1560 my $hereline = "$here\n$rawline\n";
1561 my $herecurr = "$here\n$rawline\n";
1562 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1563
1564 $cnt_lines++ if ($realcnt != 0);
1565
1566 # Check for incorrect file permissions
1567 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1568 my $permhere = $here . "FILE: $realfile\n";
1569 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1570 ERROR("EXECUTE_PERMISSIONS",
1571 "do not set execute permissions for source files\n" . $permhere);
1572 }
1573 }
1574
1575 # Check the patch for a signoff:
1576 if ($line =~ /^\s*signed-off-by:/i) {
1577 $signoff++;
1578 $in_commit_log = 0;
1579 }
1580
1581 # Check signature styles
1582 if (!$in_header_lines &&
1583 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1584 my $space_before = $1;
1585 my $sign_off = $2;
1586 my $space_after = $3;
1587 my $email = $4;
1588 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1589
1590 if (defined $space_before && $space_before ne "") {
1591 WARN("BAD_SIGN_OFF",
1592 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1593 }
1594 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1595 WARN("BAD_SIGN_OFF",
1596 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1597 }
1598 if (!defined $space_after || $space_after ne " ") {
1599 WARN("BAD_SIGN_OFF",
1600 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
1601 }
1602
1603 my ($email_name, $email_address, $comment) = parse_email($email);
1604 my $suggested_email = format_email(($email_name, $email_address));
1605 if ($suggested_email eq "") {
1606 ERROR("BAD_SIGN_OFF",
1607 "Unrecognized email address: '$email'\n" . $herecurr);
1608 } else {
1609 my $dequoted = $suggested_email;
1610 $dequoted =~ s/^"//;
1611 $dequoted =~ s/" </ </;
1612 # Don't force email to have quotes
1613 # Allow just an angle bracketed address
1614 if ("$dequoted$comment" ne $email &&
1615 "<$email_address>$comment" ne $email &&
1616 "$suggested_email$comment" ne $email) {
1617 WARN("BAD_SIGN_OFF",
1618 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1619 }
1620 }
1621 }
1622
1623 # Check for wrappage within a valid hunk of the file
1624 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1625 ERROR("CORRUPTED_PATCH",
1626 "patch seems to be corrupt (line wrapped?)\n" .
1627 $herecurr) if (!$emitted_corrupt++);
1628 }
1629
1630 # Check for absolute kernel paths.
1631 if ($tree) {
1632 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1633 my $file = $1;
1634
1635 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1636 check_absolute_file($1, $herecurr)) {
1637 #
1638 } else {
1639 check_absolute_file($file, $herecurr);
1640 }
1641 }
1642 }
1643
1644 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1645 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1646 $rawline !~ m/^$UTF8*$/) {
1647 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1648
1649 my $blank = copy_spacing($rawline);
1650 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1651 my $hereptr = "$hereline$ptr\n";
1652
1653 CHK("INVALID_UTF8",
1654 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1655 }
1656
1657 # Check if it's the start of a commit log
1658 # (not a header line and we haven't seen the patch filename)
1659 if ($in_header_lines && $realfile =~ /^$/ &&
1660 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1661 $in_header_lines = 0;
1662 $in_commit_log = 1;
1663 }
1664
1665 # Still not yet in a patch, check for any UTF-8
1666 if ($in_commit_log && $realfile =~ /^$/ &&
1667 $rawline =~ /$NON_ASCII_UTF8/) {
1668 CHK("UTF8_BEFORE_PATCH",
1669 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1670 }
1671
1672 # ignore non-hunk lines and lines being removed
1673 next if (!$hunk_line || $line =~ /^-/);
1674
1675 #trailing whitespace
1676 if ($line =~ /^\+.*\015/) {
1677 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1678 ERROR("DOS_LINE_ENDINGS",
1679 "DOS line endings\n" . $herevet);
1680
1681 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1682 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1683 ERROR("TRAILING_WHITESPACE",
1684 "trailing whitespace\n" . $herevet);
1685 $rpt_cleaners = 1;
1686 }
1687
1688 # check for Kconfig help text having a real description
1689 # Only applies when adding the entry originally, after that we do not have
1690 # sufficient context to determine whether it is indeed long enough.
1691 if ($realfile =~ /Kconfig/ &&
1692 $line =~ /.\s*config\s+/) {
1693 my $length = 0;
1694 my $cnt = $realcnt;
1695 my $ln = $linenr + 1;
1696 my $f;
1697 my $is_start = 0;
1698 my $is_end = 0;
1699 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1700 $f = $lines[$ln - 1];
1701 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1702 $is_end = $lines[$ln - 1] =~ /^\+/;
1703
1704 next if ($f =~ /^-/);
1705
1706 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1707 $is_start = 1;
1708 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1709 $length = -1;
1710 }
1711
1712 $f =~ s/^.//;
1713 $f =~ s/#.*//;
1714 $f =~ s/^\s+//;
1715 next if ($f =~ /^$/);
1716 if ($f =~ /^\s*config\s/) {
1717 $is_end = 1;
1718 last;
1719 }
1720 $length++;
1721 }
1722 WARN("CONFIG_DESCRIPTION",
1723 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1724 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1725 }
1726
1727 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1728 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1729 my $flag = $1;
1730 my $replacement = {
1731 'EXTRA_AFLAGS' => 'asflags-y',
1732 'EXTRA_CFLAGS' => 'ccflags-y',
1733 'EXTRA_CPPFLAGS' => 'cppflags-y',
1734 'EXTRA_LDFLAGS' => 'ldflags-y',
1735 };
1736
1737 WARN("DEPRECATED_VARIABLE",
1738 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1739 }
1740
1741 # check we are in a valid source file if not then ignore this hunk
1742 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1743
1744 #80 column limit
1745 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1746 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1747 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1748 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1749 $length > 80)
1750 {
1751 WARN("LONG_LINE",
1752 "line over 80 characters\n" . $herecurr);
1753 }
1754
1755 # Check for user-visible strings broken across lines, which breaks the ability
1756 # to grep for the string. Limited to strings used as parameters (those
1757 # following an open parenthesis), which almost completely eliminates false
1758 # positives, as well as warning only once per parameter rather than once per
1759 # line of the string. Make an exception when the previous string ends in a
1760 # newline (multiple lines in one string constant) or \n\t (common in inline
1761 # assembly to indent the instruction on the following line).
1762 if ($line =~ /^\+\s*"/ &&
1763 $prevline =~ /"\s*$/ &&
1764 $prevline =~ /\(/ &&
1765 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1766 WARN("SPLIT_STRING",
1767 "quoted string split across lines\n" . $hereprev);
1768 }
1769
1770 # check for spaces before a quoted newline
1771 if ($rawline =~ /^.*\".*\s\\n/) {
1772 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1773 "unnecessary whitespace before a quoted newline\n" . $herecurr);
1774 }
1775
1776 # check for adding lines without a newline.
1777 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1778 WARN("MISSING_EOF_NEWLINE",
1779 "adding a line without newline at end of file\n" . $herecurr);
1780 }
1781
1782 # Blackfin: use hi/lo macros
1783 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1784 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1785 my $herevet = "$here\n" . cat_vet($line) . "\n";
1786 ERROR("LO_MACRO",
1787 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1788 }
1789 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1790 my $herevet = "$here\n" . cat_vet($line) . "\n";
1791 ERROR("HI_MACRO",
1792 "use the HI() macro, not (... >> 16)\n" . $herevet);
1793 }
1794 }
1795
1796 # check we are in a valid source file C or perl if not then ignore this hunk
1797 next if ($realfile !~ /\.(h|c|pl)$/);
1798
1799 # at the beginning of a line any tabs must come first and anything
1800 # more than 8 must use tabs.
1801 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1802 $rawline =~ /^\+\s* \s*/) {
1803 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1804 ERROR("CODE_INDENT",
1805 "code indent should use tabs where possible\n" . $herevet);
1806 $rpt_cleaners = 1;
1807 }
1808
1809 # check for space before tabs.
1810 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1811 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1812 WARN("SPACE_BEFORE_TAB",
1813 "please, no space before tabs\n" . $herevet);
1814 }
1815
1816 # check for && or || at the start of a line
1817 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1818 CHK("LOGICAL_CONTINUATIONS",
1819 "Logical continuations should be on the previous line\n" . $hereprev);
1820 }
1821
1822 # check multi-line statement indentation matches previous line
1823 if ($^V && $^V ge 5.10.0 &&
1824 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1825 $prevline =~ /^\+(\t*)(.*)$/;
1826 my $oldindent = $1;
1827 my $rest = $2;
1828
1829 my $pos = pos_last_openparen($rest);
1830 if ($pos >= 0) {
1831 $line =~ /^\+([ \t]*)/;
1832 my $newindent = $1;
1833
1834 my $goodtabindent = $oldindent .
1835 "\t" x ($pos / 8) .
1836 " " x ($pos % 8);
1837 my $goodspaceindent = $oldindent . " " x $pos;
1838
1839 if ($newindent ne $goodtabindent &&
1840 $newindent ne $goodspaceindent) {
1841 CHK("PARENTHESIS_ALIGNMENT",
1842 "Alignment should match open parenthesis\n" . $hereprev);
1843 }
1844 }
1845 }
1846
1847 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1848 CHK("SPACING",
1849 "No space is necessary after a cast\n" . $hereprev);
1850 }
1851
1852 if ($rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1853 $prevrawline =~ /^\+[ \t]*$/) {
1854 CHK("BLOCK_COMMENT_STYLE",
1855 "Don't begin block comments with only a /* line, use /* comment...\n" . $hereprev);
1856 }
1857
1858 # check for spaces at the beginning of a line.
1859 # Exceptions:
1860 # 1) within comments
1861 # 2) indented preprocessor commands
1862 # 3) hanging labels
1863 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1864 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1865 WARN("LEADING_SPACE",
1866 "please, no spaces at the start of a line\n" . $herevet);
1867 }
1868
1869 # check we are in a valid C source file if not then ignore this hunk
1870 next if ($realfile !~ /\.(h|c)$/);
1871
1872 # check for RCS/CVS revision markers
1873 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1874 WARN("CVS_KEYWORD",
1875 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1876 }
1877
1878 # Blackfin: don't use __builtin_bfin_[cs]sync
1879 if ($line =~ /__builtin_bfin_csync/) {
1880 my $herevet = "$here\n" . cat_vet($line) . "\n";
1881 ERROR("CSYNC",
1882 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1883 }
1884 if ($line =~ /__builtin_bfin_ssync/) {
1885 my $herevet = "$here\n" . cat_vet($line) . "\n";
1886 ERROR("SSYNC",
1887 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1888 }
1889
1890 # Check for potential 'bare' types
1891 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1892 $realline_next);
1893 #print "LINE<$line>\n";
1894 if ($linenr >= $suppress_statement &&
1895 $realcnt && $line =~ /.\s*\S/) {
1896 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1897 ctx_statement_block($linenr, $realcnt, 0);
1898 $stat =~ s/\n./\n /g;
1899 $cond =~ s/\n./\n /g;
1900
1901 #print "linenr<$linenr> <$stat>\n";
1902 # If this statement has no statement boundaries within
1903 # it there is no point in retrying a statement scan
1904 # until we hit end of it.
1905 my $frag = $stat; $frag =~ s/;+\s*$//;
1906 if ($frag !~ /(?:{|;)/) {
1907 #print "skip<$line_nr_next>\n";
1908 $suppress_statement = $line_nr_next;
1909 }
1910
1911 # Find the real next line.
1912 $realline_next = $line_nr_next;
1913 if (defined $realline_next &&
1914 (!defined $lines[$realline_next - 1] ||
1915 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1916 $realline_next++;
1917 }
1918
1919 my $s = $stat;
1920 $s =~ s/{.*$//s;
1921
1922 # Ignore goto labels.
1923 if ($s =~ /$Ident:\*$/s) {
1924
1925 # Ignore functions being called
1926 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1927
1928 } elsif ($s =~ /^.\s*else\b/s) {
1929
1930 # declarations always start with types
1931 } 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) {
1932 my $type = $1;
1933 $type =~ s/\s+/ /g;
1934 possible($type, "A:" . $s);
1935
1936 # definitions in global scope can only start with types
1937 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1938 possible($1, "B:" . $s);
1939 }
1940
1941 # any (foo ... *) is a pointer cast, and foo is a type
1942 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1943 possible($1, "C:" . $s);
1944 }
1945
1946 # Check for any sort of function declaration.
1947 # int foo(something bar, other baz);
1948 # void (*store_gdt)(x86_descr_ptr *);
1949 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1950 my ($name_len) = length($1);
1951
1952 my $ctx = $s;
1953 substr($ctx, 0, $name_len + 1, '');
1954 $ctx =~ s/\)[^\)]*$//;
1955
1956 for my $arg (split(/\s*,\s*/, $ctx)) {
1957 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1958
1959 possible($1, "D:" . $s);
1960 }
1961 }
1962 }
1963
1964 }
1965
1966 #
1967 # Checks which may be anchored in the context.
1968 #
1969
1970 # Check for switch () and associated case and default
1971 # statements should be at the same indent.
1972 if ($line=~/\bswitch\s*\(.*\)/) {
1973 my $err = '';
1974 my $sep = '';
1975 my @ctx = ctx_block_outer($linenr, $realcnt);
1976 shift(@ctx);
1977 for my $ctx (@ctx) {
1978 my ($clen, $cindent) = line_stats($ctx);
1979 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1980 $indent != $cindent) {
1981 $err .= "$sep$ctx\n";
1982 $sep = '';
1983 } else {
1984 $sep = "[...]\n";
1985 }
1986 }
1987 if ($err ne '') {
1988 ERROR("SWITCH_CASE_INDENT_LEVEL",
1989 "switch and case should be at the same indent\n$hereline$err");
1990 }
1991 }
1992
1993 # if/while/etc brace do not go on next line, unless defining a do while loop,
1994 # or if that brace on the next line is for something else
1995 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1996 my $pre_ctx = "$1$2";
1997
1998 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1999
2000 if ($line =~ /^\+\t{6,}/) {
2001 WARN("DEEP_INDENTATION",
2002 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2003 }
2004
2005 my $ctx_cnt = $realcnt - $#ctx - 1;
2006 my $ctx = join("\n", @ctx);
2007
2008 my $ctx_ln = $linenr;
2009 my $ctx_skip = $realcnt;
2010
2011 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2012 defined $lines[$ctx_ln - 1] &&
2013 $lines[$ctx_ln - 1] =~ /^-/)) {
2014 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2015 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2016 $ctx_ln++;
2017 }
2018
2019 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2020 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2021
2022 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2023 ERROR("OPEN_BRACE",
2024 "that open brace { should be on the previous line\n" .
2025 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2026 }
2027 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2028 $ctx =~ /\)\s*\;\s*$/ &&
2029 defined $lines[$ctx_ln - 1])
2030 {
2031 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2032 if ($nindent > $indent) {
2033 WARN("TRAILING_SEMICOLON",
2034 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2035 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2036 }
2037 }
2038 }
2039
2040 # Check relative indent for conditionals and blocks.
2041 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2042 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2043 ctx_statement_block($linenr, $realcnt, 0)
2044 if (!defined $stat);
2045 my ($s, $c) = ($stat, $cond);
2046
2047 substr($s, 0, length($c), '');
2048
2049 # Make sure we remove the line prefixes as we have
2050 # none on the first line, and are going to readd them
2051 # where necessary.
2052 $s =~ s/\n./\n/gs;
2053
2054 # Find out how long the conditional actually is.
2055 my @newlines = ($c =~ /\n/gs);
2056 my $cond_lines = 1 + $#newlines;
2057
2058 # We want to check the first line inside the block
2059 # starting at the end of the conditional, so remove:
2060 # 1) any blank line termination
2061 # 2) any opening brace { on end of the line
2062 # 3) any do (...) {
2063 my $continuation = 0;
2064 my $check = 0;
2065 $s =~ s/^.*\bdo\b//;
2066 $s =~ s/^\s*{//;
2067 if ($s =~ s/^\s*\\//) {
2068 $continuation = 1;
2069 }
2070 if ($s =~ s/^\s*?\n//) {
2071 $check = 1;
2072 $cond_lines++;
2073 }
2074
2075 # Also ignore a loop construct at the end of a
2076 # preprocessor statement.
2077 if (($prevline =~ /^.\s*#\s*define\s/ ||
2078 $prevline =~ /\\\s*$/) && $continuation == 0) {
2079 $check = 0;
2080 }
2081
2082 my $cond_ptr = -1;
2083 $continuation = 0;
2084 while ($cond_ptr != $cond_lines) {
2085 $cond_ptr = $cond_lines;
2086
2087 # If we see an #else/#elif then the code
2088 # is not linear.
2089 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2090 $check = 0;
2091 }
2092
2093 # Ignore:
2094 # 1) blank lines, they should be at 0,
2095 # 2) preprocessor lines, and
2096 # 3) labels.
2097 if ($continuation ||
2098 $s =~ /^\s*?\n/ ||
2099 $s =~ /^\s*#\s*?/ ||
2100 $s =~ /^\s*$Ident\s*:/) {
2101 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2102 if ($s =~ s/^.*?\n//) {
2103 $cond_lines++;
2104 }
2105 }
2106 }
2107
2108 my (undef, $sindent) = line_stats("+" . $s);
2109 my $stat_real = raw_line($linenr, $cond_lines);
2110
2111 # Check if either of these lines are modified, else
2112 # this is not this patch's fault.
2113 if (!defined($stat_real) ||
2114 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2115 $check = 0;
2116 }
2117 if (defined($stat_real) && $cond_lines > 1) {
2118 $stat_real = "[...]\n$stat_real";
2119 }
2120
2121 #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";
2122
2123 if ($check && (($sindent % 8) != 0 ||
2124 ($sindent <= $indent && $s ne ''))) {
2125 WARN("SUSPECT_CODE_INDENT",
2126 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2127 }
2128 }
2129
2130 # Track the 'values' across context and added lines.
2131 my $opline = $line; $opline =~ s/^./ /;
2132 my ($curr_values, $curr_vars) =
2133 annotate_values($opline . "\n", $prev_values);
2134 $curr_values = $prev_values . $curr_values;
2135 if ($dbg_values) {
2136 my $outline = $opline; $outline =~ s/\t/ /g;
2137 print "$linenr > .$outline\n";
2138 print "$linenr > $curr_values\n";
2139 print "$linenr > $curr_vars\n";
2140 }
2141 $prev_values = substr($curr_values, -1);
2142
2143 #ignore lines not being added
2144 if ($line=~/^[^\+]/) {next;}
2145
2146 # TEST: allow direct testing of the type matcher.
2147 if ($dbg_type) {
2148 if ($line =~ /^.\s*$Declare\s*$/) {
2149 ERROR("TEST_TYPE",
2150 "TEST: is type\n" . $herecurr);
2151 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2152 ERROR("TEST_NOT_TYPE",
2153 "TEST: is not type ($1 is)\n". $herecurr);
2154 }
2155 next;
2156 }
2157 # TEST: allow direct testing of the attribute matcher.
2158 if ($dbg_attr) {
2159 if ($line =~ /^.\s*$Modifier\s*$/) {
2160 ERROR("TEST_ATTR",
2161 "TEST: is attr\n" . $herecurr);
2162 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2163 ERROR("TEST_NOT_ATTR",
2164 "TEST: is not attr ($1 is)\n". $herecurr);
2165 }
2166 next;
2167 }
2168
2169 # check for initialisation to aggregates open brace on the next line
2170 if ($line =~ /^.\s*{/ &&
2171 $prevline =~ /(?:^|[^=])=\s*$/) {
2172 ERROR("OPEN_BRACE",
2173 "that open brace { should be on the previous line\n" . $hereprev);
2174 }
2175
2176 #
2177 # Checks which are anchored on the added line.
2178 #
2179
2180 # check for malformed paths in #include statements (uses RAW line)
2181 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2182 my $path = $1;
2183 if ($path =~ m{//}) {
2184 ERROR("MALFORMED_INCLUDE",
2185 "malformed #include filename\n" .
2186 $herecurr);
2187 }
2188 }
2189
2190 # no C99 // comments
2191 if ($line =~ m{//}) {
2192 ERROR("C99_COMMENTS",
2193 "do not use C99 // comments\n" . $herecurr);
2194 }
2195 # Remove C99 comments.
2196 $line =~ s@//.*@@;
2197 $opline =~ s@//.*@@;
2198
2199 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2200 # the whole statement.
2201 #print "APW <$lines[$realline_next - 1]>\n";
2202 if (defined $realline_next &&
2203 exists $lines[$realline_next - 1] &&
2204 !defined $suppress_export{$realline_next} &&
2205 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2206 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2207 # Handle definitions which produce identifiers with
2208 # a prefix:
2209 # XXX(foo);
2210 # EXPORT_SYMBOL(something_foo);
2211 my $name = $1;
2212 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2213 $name =~ /^${Ident}_$2/) {
2214 #print "FOO C name<$name>\n";
2215 $suppress_export{$realline_next} = 1;
2216
2217 } elsif ($stat !~ /(?:
2218 \n.}\s*$|
2219 ^.DEFINE_$Ident\(\Q$name\E\)|
2220 ^.DECLARE_$Ident\(\Q$name\E\)|
2221 ^.LIST_HEAD\(\Q$name\E\)|
2222 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2223 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2224 )/x) {
2225 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2226 $suppress_export{$realline_next} = 2;
2227 } else {
2228 $suppress_export{$realline_next} = 1;
2229 }
2230 }
2231 if (!defined $suppress_export{$linenr} &&
2232 $prevline =~ /^.\s*$/ &&
2233 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2234 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2235 #print "FOO B <$lines[$linenr - 1]>\n";
2236 $suppress_export{$linenr} = 2;
2237 }
2238 if (defined $suppress_export{$linenr} &&
2239 $suppress_export{$linenr} == 2) {
2240 WARN("EXPORT_SYMBOL",
2241 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2242 }
2243
2244 # check for global initialisers.
2245 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2246 ERROR("GLOBAL_INITIALISERS",
2247 "do not initialise globals to 0 or NULL\n" .
2248 $herecurr);
2249 }
2250 # check for static initialisers.
2251 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2252 ERROR("INITIALISED_STATIC",
2253 "do not initialise statics to 0 or NULL\n" .
2254 $herecurr);
2255 }
2256
2257 # check for static const char * arrays.
2258 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2259 WARN("STATIC_CONST_CHAR_ARRAY",
2260 "static const char * array should probably be static const char * const\n" .
2261 $herecurr);
2262 }
2263
2264 # check for static char foo[] = "bar" declarations.
2265 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2266 WARN("STATIC_CONST_CHAR_ARRAY",
2267 "static char array declaration should probably be static const char\n" .
2268 $herecurr);
2269 }
2270
2271 # check for declarations of struct pci_device_id
2272 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2273 WARN("DEFINE_PCI_DEVICE_TABLE",
2274 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2275 }
2276
2277 # check for new typedefs, only function parameters and sparse annotations
2278 # make sense.
2279 if ($line =~ /\btypedef\s/ &&
2280 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2281 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2282 $line !~ /\b$typeTypedefs\b/ &&
2283 $line !~ /\b__bitwise(?:__|)\b/) {
2284 WARN("NEW_TYPEDEFS",
2285 "do not add new typedefs\n" . $herecurr);
2286 }
2287
2288 # * goes on variable not on type
2289 # (char*[ const])
2290 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2291 #print "AA<$1>\n";
2292 my ($from, $to) = ($2, $2);
2293
2294 # Should start with a space.
2295 $to =~ s/^(\S)/ $1/;
2296 # Should not end with a space.
2297 $to =~ s/\s+$//;
2298 # '*'s should not have spaces between.
2299 while ($to =~ s/\*\s+\*/\*\*/) {
2300 }
2301
2302 #print "from<$from> to<$to>\n";
2303 if ($from ne $to) {
2304 ERROR("POINTER_LOCATION",
2305 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2306 }
2307 }
2308 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2309 #print "BB<$1>\n";
2310 my ($from, $to, $ident) = ($2, $2, $3);
2311
2312 # Should start with a space.
2313 $to =~ s/^(\S)/ $1/;
2314 # Should not end with a space.
2315 $to =~ s/\s+$//;
2316 # '*'s should not have spaces between.
2317 while ($to =~ s/\*\s+\*/\*\*/) {
2318 }
2319 # Modifiers should have spaces.
2320 $to =~ s/(\b$Modifier$)/$1 /;
2321
2322 #print "from<$from> to<$to> ident<$ident>\n";
2323 if ($from ne $to && $ident !~ /^$Modifier$/) {
2324 ERROR("POINTER_LOCATION",
2325 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2326 }
2327 }
2328
2329 # # no BUG() or BUG_ON()
2330 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2331 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2332 # print "$herecurr";
2333 # $clean = 0;
2334 # }
2335
2336 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2337 WARN("LINUX_VERSION_CODE",
2338 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2339 }
2340
2341 # check for uses of printk_ratelimit
2342 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2343 WARN("PRINTK_RATELIMITED",
2344 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2345 }
2346
2347 # printk should use KERN_* levels. Note that follow on printk's on the
2348 # same line do not need a level, so we use the current block context
2349 # to try and find and validate the current printk. In summary the current
2350 # printk includes all preceding printk's which have no newline on the end.
2351 # we assume the first bad printk is the one to report.
2352 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2353 my $ok = 0;
2354 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2355 #print "CHECK<$lines[$ln - 1]\n";
2356 # we have a preceding printk if it ends
2357 # with "\n" ignore it, else it is to blame
2358 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2359 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2360 $ok = 1;
2361 }
2362 last;
2363 }
2364 }
2365 if ($ok == 0) {
2366 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2367 "printk() should include KERN_ facility level\n" . $herecurr);
2368 }
2369 }
2370
2371 # function brace can't be on same line, except for #defines of do while,
2372 # or if closed on same line
2373 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2374 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2375 ERROR("OPEN_BRACE",
2376 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2377 }
2378
2379 # open braces for enum, union and struct go on the same line.
2380 if ($line =~ /^.\s*{/ &&
2381 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2382 ERROR("OPEN_BRACE",
2383 "open brace '{' following $1 go on the same line\n" . $hereprev);
2384 }
2385
2386 # missing space after union, struct or enum definition
2387 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2388 WARN("SPACING",
2389 "missing space after $1 definition\n" . $herecurr);
2390 }
2391
2392 # check for spacing round square brackets; allowed:
2393 # 1. with a type on the left -- int [] a;
2394 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2395 # 3. inside a curly brace -- = { [0...10] = 5 }
2396 while ($line =~ /(.*?\s)\[/g) {
2397 my ($where, $prefix) = ($-[1], $1);
2398 if ($prefix !~ /$Type\s+$/ &&
2399 ($where != 0 || $prefix !~ /^.\s+$/) &&
2400 $prefix !~ /[{,]\s+$/) {
2401 ERROR("BRACKET_SPACE",
2402 "space prohibited before open square bracket '['\n" . $herecurr);
2403 }
2404 }
2405
2406 # check for spaces between functions and their parentheses.
2407 while ($line =~ /($Ident)\s+\(/g) {
2408 my $name = $1;
2409 my $ctx_before = substr($line, 0, $-[1]);
2410 my $ctx = "$ctx_before$name";
2411
2412 # Ignore those directives where spaces _are_ permitted.
2413 if ($name =~ /^(?:
2414 if|for|while|switch|return|case|
2415 volatile|__volatile__|
2416 __attribute__|format|__extension__|
2417 asm|__asm__)$/x)
2418 {
2419
2420 # cpp #define statements have non-optional spaces, ie
2421 # if there is a space between the name and the open
2422 # parenthesis it is simply not a parameter group.
2423 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2424
2425 # cpp #elif statement condition may start with a (
2426 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2427
2428 # If this whole things ends with a type its most
2429 # likely a typedef for a function.
2430 } elsif ($ctx =~ /$Type$/) {
2431
2432 } else {
2433 WARN("SPACING",
2434 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2435 }
2436 }
2437 # Check operator spacing.
2438 if (!($line=~/\#\s*include/)) {
2439 my $ops = qr{
2440 <<=|>>=|<=|>=|==|!=|
2441 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2442 =>|->|<<|>>|<|>|=|!|~|
2443 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2444 \?|:
2445 }x;
2446 my @elements = split(/($ops|;)/, $opline);
2447 my $off = 0;
2448
2449 my $blank = copy_spacing($opline);
2450
2451 for (my $n = 0; $n < $#elements; $n += 2) {
2452 $off += length($elements[$n]);
2453
2454 # Pick up the preceding and succeeding characters.
2455 my $ca = substr($opline, 0, $off);
2456 my $cc = '';
2457 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2458 $cc = substr($opline, $off + length($elements[$n + 1]));
2459 }
2460 my $cb = "$ca$;$cc";
2461
2462 my $a = '';
2463 $a = 'V' if ($elements[$n] ne '');
2464 $a = 'W' if ($elements[$n] =~ /\s$/);
2465 $a = 'C' if ($elements[$n] =~ /$;$/);
2466 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2467 $a = 'O' if ($elements[$n] eq '');
2468 $a = 'E' if ($ca =~ /^\s*$/);
2469
2470 my $op = $elements[$n + 1];
2471
2472 my $c = '';
2473 if (defined $elements[$n + 2]) {
2474 $c = 'V' if ($elements[$n + 2] ne '');
2475 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2476 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2477 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2478 $c = 'O' if ($elements[$n + 2] eq '');
2479 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2480 } else {
2481 $c = 'E';
2482 }
2483
2484 my $ctx = "${a}x${c}";
2485
2486 my $at = "(ctx:$ctx)";
2487
2488 my $ptr = substr($blank, 0, $off) . "^";
2489 my $hereptr = "$hereline$ptr\n";
2490
2491 # Pull out the value of this operator.
2492 my $op_type = substr($curr_values, $off + 1, 1);
2493
2494 # Get the full operator variant.
2495 my $opv = $op . substr($curr_vars, $off, 1);
2496
2497 # Ignore operators passed as parameters.
2498 if ($op_type ne 'V' &&
2499 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2500
2501 # # Ignore comments
2502 # } elsif ($op =~ /^$;+$/) {
2503
2504 # ; should have either the end of line or a space or \ after it
2505 } elsif ($op eq ';') {
2506 if ($ctx !~ /.x[WEBC]/ &&
2507 $cc !~ /^\\/ && $cc !~ /^;/) {
2508 ERROR("SPACING",
2509 "space required after that '$op' $at\n" . $hereptr);
2510 }
2511
2512 # // is a comment
2513 } elsif ($op eq '//') {
2514
2515 # No spaces for:
2516 # ->
2517 # : when part of a bitfield
2518 } elsif ($op eq '->' || $opv eq ':B') {
2519 if ($ctx =~ /Wx.|.xW/) {
2520 ERROR("SPACING",
2521 "spaces prohibited around that '$op' $at\n" . $hereptr);
2522 }
2523
2524 # , must have a space on the right.
2525 } elsif ($op eq ',') {
2526 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2527 ERROR("SPACING",
2528 "space required after that '$op' $at\n" . $hereptr);
2529 }
2530
2531 # '*' as part of a type definition -- reported already.
2532 } elsif ($opv eq '*_') {
2533 #warn "'*' is part of type\n";
2534
2535 # unary operators should have a space before and
2536 # none after. May be left adjacent to another
2537 # unary operator, or a cast
2538 } elsif ($op eq '!' || $op eq '~' ||
2539 $opv eq '*U' || $opv eq '-U' ||
2540 $opv eq '&U' || $opv eq '&&U') {
2541 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2542 ERROR("SPACING",
2543 "space required before that '$op' $at\n" . $hereptr);
2544 }
2545 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2546 # A unary '*' may be const
2547
2548 } elsif ($ctx =~ /.xW/) {
2549 ERROR("SPACING",
2550 "space prohibited after that '$op' $at\n" . $hereptr);
2551 }
2552
2553 # unary ++ and unary -- are allowed no space on one side.
2554 } elsif ($op eq '++' or $op eq '--') {
2555 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2556 ERROR("SPACING",
2557 "space required one side of that '$op' $at\n" . $hereptr);
2558 }
2559 if ($ctx =~ /Wx[BE]/ ||
2560 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2561 ERROR("SPACING",
2562 "space prohibited before that '$op' $at\n" . $hereptr);
2563 }
2564 if ($ctx =~ /ExW/) {
2565 ERROR("SPACING",
2566 "space prohibited after that '$op' $at\n" . $hereptr);
2567 }
2568
2569
2570 # << and >> may either have or not have spaces both sides
2571 } elsif ($op eq '<<' or $op eq '>>' or
2572 $op eq '&' or $op eq '^' or $op eq '|' or
2573 $op eq '+' or $op eq '-' or
2574 $op eq '*' or $op eq '/' or
2575 $op eq '%')
2576 {
2577 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2578 ERROR("SPACING",
2579 "need consistent spacing around '$op' $at\n" .
2580 $hereptr);
2581 }
2582
2583 # A colon needs no spaces before when it is
2584 # terminating a case value or a label.
2585 } elsif ($opv eq ':C' || $opv eq ':L') {
2586 if ($ctx =~ /Wx./) {
2587 ERROR("SPACING",
2588 "space prohibited before that '$op' $at\n" . $hereptr);
2589 }
2590
2591 # All the others need spaces both sides.
2592 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2593 my $ok = 0;
2594
2595 # Ignore email addresses <foo@bar>
2596 if (($op eq '<' &&
2597 $cc =~ /^\S+\@\S+>/) ||
2598 ($op eq '>' &&
2599 $ca =~ /<\S+\@\S+$/))
2600 {
2601 $ok = 1;
2602 }
2603
2604 # Ignore ?:
2605 if (($opv eq ':O' && $ca =~ /\?$/) ||
2606 ($op eq '?' && $cc =~ /^:/)) {
2607 $ok = 1;
2608 }
2609
2610 if ($ok == 0) {
2611 ERROR("SPACING",
2612 "spaces required around that '$op' $at\n" . $hereptr);
2613 }
2614 }
2615 $off += length($elements[$n + 1]);
2616 }
2617 }
2618
2619 # check for multiple assignments
2620 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2621 CHK("MULTIPLE_ASSIGNMENTS",
2622 "multiple assignments should be avoided\n" . $herecurr);
2623 }
2624
2625 ## # check for multiple declarations, allowing for a function declaration
2626 ## # continuation.
2627 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2628 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2629 ##
2630 ## # Remove any bracketed sections to ensure we do not
2631 ## # falsly report the parameters of functions.
2632 ## my $ln = $line;
2633 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2634 ## }
2635 ## if ($ln =~ /,/) {
2636 ## WARN("MULTIPLE_DECLARATION",
2637 ## "declaring multiple variables together should be avoided\n" . $herecurr);
2638 ## }
2639 ## }
2640
2641 #need space before brace following if, while, etc
2642 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2643 $line =~ /do{/) {
2644 ERROR("SPACING",
2645 "space required before the open brace '{'\n" . $herecurr);
2646 }
2647
2648 # closing brace should have a space following it when it has anything
2649 # on the line
2650 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2651 ERROR("SPACING",
2652 "space required after that close brace '}'\n" . $herecurr);
2653 }
2654
2655 # check spacing on square brackets
2656 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2657 ERROR("SPACING",
2658 "space prohibited after that open square bracket '['\n" . $herecurr);
2659 }
2660 if ($line =~ /\s\]/) {
2661 ERROR("SPACING",
2662 "space prohibited before that close square bracket ']'\n" . $herecurr);
2663 }
2664
2665 # check spacing on parentheses
2666 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2667 $line !~ /for\s*\(\s+;/) {
2668 ERROR("SPACING",
2669 "space prohibited after that open parenthesis '('\n" . $herecurr);
2670 }
2671 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2672 $line !~ /for\s*\(.*;\s+\)/ &&
2673 $line !~ /:\s+\)/) {
2674 ERROR("SPACING",
2675 "space prohibited before that close parenthesis ')'\n" . $herecurr);
2676 }
2677
2678 #goto labels aren't indented, allow a single space however
2679 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2680 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2681 WARN("INDENTED_LABEL",
2682 "labels should not be indented\n" . $herecurr);
2683 }
2684
2685 # Return is not a function.
2686 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2687 my $spacing = $1;
2688 my $value = $2;
2689
2690 # Flatten any parentheses
2691 $value =~ s/\(/ \(/g;
2692 $value =~ s/\)/\) /g;
2693 while ($value =~ s/\[[^\[\]]*\]/1/ ||
2694 $value !~ /(?:$Ident|-?$Constant)\s*
2695 $Compare\s*
2696 (?:$Ident|-?$Constant)/x &&
2697 $value =~ s/\([^\(\)]*\)/1/) {
2698 }
2699 #print "value<$value>\n";
2700 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2701 ERROR("RETURN_PARENTHESES",
2702 "return is not a function, parentheses are not required\n" . $herecurr);
2703
2704 } elsif ($spacing !~ /\s+/) {
2705 ERROR("SPACING",
2706 "space required before the open parenthesis '('\n" . $herecurr);
2707 }
2708 }
2709 # Return of what appears to be an errno should normally be -'ve
2710 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2711 my $name = $1;
2712 if ($name ne 'EOF' && $name ne 'ERROR') {
2713 WARN("USE_NEGATIVE_ERRNO",
2714 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2715 }
2716 }
2717
2718 # Need a space before open parenthesis after if, while etc
2719 if ($line=~/\b(if|while|for|switch)\(/) {
2720 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
2721 }
2722
2723 # Check for illegal assignment in if conditional -- and check for trailing
2724 # statements after the conditional.
2725 if ($line =~ /do\s*(?!{)/) {
2726 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2727 ctx_statement_block($linenr, $realcnt, 0)
2728 if (!defined $stat);
2729 my ($stat_next) = ctx_statement_block($line_nr_next,
2730 $remain_next, $off_next);
2731 $stat_next =~ s/\n./\n /g;
2732 ##print "stat<$stat> stat_next<$stat_next>\n";
2733
2734 if ($stat_next =~ /^\s*while\b/) {
2735 # If the statement carries leading newlines,
2736 # then count those as offsets.
2737 my ($whitespace) =
2738 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2739 my $offset =
2740 statement_rawlines($whitespace) - 1;
2741
2742 $suppress_whiletrailers{$line_nr_next +
2743 $offset} = 1;
2744 }
2745 }
2746 if (!defined $suppress_whiletrailers{$linenr} &&
2747 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2748 my ($s, $c) = ($stat, $cond);
2749
2750 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2751 ERROR("ASSIGN_IN_IF",
2752 "do not use assignment in if condition\n" . $herecurr);
2753 }
2754
2755 # Find out what is on the end of the line after the
2756 # conditional.
2757 substr($s, 0, length($c), '');
2758 $s =~ s/\n.*//g;
2759 $s =~ s/$;//g; # Remove any comments
2760 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2761 $c !~ /}\s*while\s*/)
2762 {
2763 # Find out how long the conditional actually is.
2764 my @newlines = ($c =~ /\n/gs);
2765 my $cond_lines = 1 + $#newlines;
2766 my $stat_real = '';
2767
2768 $stat_real = raw_line($linenr, $cond_lines)
2769 . "\n" if ($cond_lines);
2770 if (defined($stat_real) && $cond_lines > 1) {
2771 $stat_real = "[...]\n$stat_real";
2772 }
2773
2774 ERROR("TRAILING_STATEMENTS",
2775 "trailing statements should be on next line\n" . $herecurr . $stat_real);
2776 }
2777 }
2778
2779 # Check for bitwise tests written as boolean
2780 if ($line =~ /
2781 (?:
2782 (?:\[|\(|\&\&|\|\|)
2783 \s*0[xX][0-9]+\s*
2784 (?:\&\&|\|\|)
2785 |
2786 (?:\&\&|\|\|)
2787 \s*0[xX][0-9]+\s*
2788 (?:\&\&|\|\||\)|\])
2789 )/x)
2790 {
2791 WARN("HEXADECIMAL_BOOLEAN_TEST",
2792 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2793 }
2794
2795 # if and else should not have general statements after it
2796 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2797 my $s = $1;
2798 $s =~ s/$;//g; # Remove any comments
2799 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2800 ERROR("TRAILING_STATEMENTS",
2801 "trailing statements should be on next line\n" . $herecurr);
2802 }
2803 }
2804 # if should not continue a brace
2805 if ($line =~ /}\s*if\b/) {
2806 ERROR("TRAILING_STATEMENTS",
2807 "trailing statements should be on next line\n" .
2808 $herecurr);
2809 }
2810 # case and default should not have general statements after them
2811 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2812 $line !~ /\G(?:
2813 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2814 \s*return\s+
2815 )/xg)
2816 {
2817 ERROR("TRAILING_STATEMENTS",
2818 "trailing statements should be on next line\n" . $herecurr);
2819 }
2820
2821 # Check for }<nl>else {, these must be at the same
2822 # indent level to be relevant to each other.
2823 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2824 $previndent == $indent) {
2825 ERROR("ELSE_AFTER_BRACE",
2826 "else should follow close brace '}'\n" . $hereprev);
2827 }
2828
2829 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2830 $previndent == $indent) {
2831 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2832
2833 # Find out what is on the end of the line after the
2834 # conditional.
2835 substr($s, 0, length($c), '');
2836 $s =~ s/\n.*//g;
2837
2838 if ($s =~ /^\s*;/) {
2839 ERROR("WHILE_AFTER_BRACE",
2840 "while should follow close brace '}'\n" . $hereprev);
2841 }
2842 }
2843
2844 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2845 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2846 # print "No studly caps, use _\n";
2847 # print "$herecurr";
2848 # $clean = 0;
2849 # }
2850
2851 #no spaces allowed after \ in define
2852 if ($line=~/\#\s*define.*\\\s$/) {
2853 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2854 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
2855 }
2856
2857 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2858 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2859 my $file = "$1.h";
2860 my $checkfile = "include/linux/$file";
2861 if (-f "$root/$checkfile" &&
2862 $realfile ne $checkfile &&
2863 $1 !~ /$allowed_asm_includes/)
2864 {
2865 if ($realfile =~ m{^arch/}) {
2866 CHK("ARCH_INCLUDE_LINUX",
2867 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2868 } else {
2869 WARN("INCLUDE_LINUX",
2870 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2871 }
2872 }
2873 }
2874
2875 # multi-statement macros should be enclosed in a do while loop, grab the
2876 # first statement and ensure its the whole macro if its not enclosed
2877 # in a known good container
2878 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2879 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2880 my $ln = $linenr;
2881 my $cnt = $realcnt;
2882 my ($off, $dstat, $dcond, $rest);
2883 my $ctx = '';
2884 ($dstat, $dcond, $ln, $cnt, $off) =
2885 ctx_statement_block($linenr, $realcnt, 0);
2886 $ctx = $dstat;
2887 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2888 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2889
2890 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2891 $dstat =~ s/$;//g;
2892 $dstat =~ s/\\\n.//g;
2893 $dstat =~ s/^\s*//s;
2894 $dstat =~ s/\s*$//s;
2895
2896 # Flatten any parentheses and braces
2897 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2898 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2899 $dstat =~ s/\[[^\[\]]*\]/1/)
2900 {
2901 }
2902
2903 # Flatten any obvious string concatentation.
2904 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
2905 $dstat =~ s/$Ident\s*("X*")/$1/)
2906 {
2907 }
2908
2909 my $exceptions = qr{
2910 $Declare|
2911 module_param_named|
2912 MODULE_PARAM_DESC|
2913 DECLARE_PER_CPU|
2914 DEFINE_PER_CPU|
2915 __typeof__\(|
2916 union|
2917 struct|
2918 \.$Ident\s*=\s*|
2919 ^\"|\"$
2920 }x;
2921 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2922 if ($dstat ne '' &&
2923 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
2924 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
2925 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
2926 $dstat !~ /^'X'$/ && # character constants
2927 $dstat !~ /$exceptions/ &&
2928 $dstat !~ /^\.$Ident\s*=/ && # .foo =
2929 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
2930 $dstat !~ /^for\s*$Constant$/ && # for (...)
2931 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
2932 $dstat !~ /^do\s*{/ && # do {...
2933 $dstat !~ /^\({/) # ({...
2934 {
2935 $ctx =~ s/\n*$//;
2936 my $herectx = $here . "\n";
2937 my $cnt = statement_rawlines($ctx);
2938
2939 for (my $n = 0; $n < $cnt; $n++) {
2940 $herectx .= raw_line($linenr, $n) . "\n";
2941 }
2942
2943 if ($dstat =~ /;/) {
2944 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2945 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
2946 } else {
2947 ERROR("COMPLEX_MACRO",
2948 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
2949 }
2950 }
2951 }
2952
2953 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2954 # all assignments may have only one of the following with an assignment:
2955 # .
2956 # ALIGN(...)
2957 # VMLINUX_SYMBOL(...)
2958 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2959 WARN("MISSING_VMLINUX_SYMBOL",
2960 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2961 }
2962
2963 # check for redundant bracing round if etc
2964 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2965 my ($level, $endln, @chunks) =
2966 ctx_statement_full($linenr, $realcnt, 1);
2967 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2968 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2969 if ($#chunks > 0 && $level == 0) {
2970 my @allowed = ();
2971 my $allow = 0;
2972 my $seen = 0;
2973 my $herectx = $here . "\n";
2974 my $ln = $linenr - 1;
2975 for my $chunk (@chunks) {
2976 my ($cond, $block) = @{$chunk};
2977
2978 # If the condition carries leading newlines, then count those as offsets.
2979 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2980 my $offset = statement_rawlines($whitespace) - 1;
2981
2982 $allowed[$allow] = 0;
2983 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2984
2985 # We have looked at and allowed this specific line.
2986 $suppress_ifbraces{$ln + $offset} = 1;
2987
2988 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2989 $ln += statement_rawlines($block) - 1;
2990
2991 substr($block, 0, length($cond), '');
2992
2993 $seen++ if ($block =~ /^\s*{/);
2994
2995 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
2996 if (statement_lines($cond) > 1) {
2997 #print "APW: ALLOWED: cond<$cond>\n";
2998 $allowed[$allow] = 1;
2999 }
3000 if ($block =~/\b(?:if|for|while)\b/) {
3001 #print "APW: ALLOWED: block<$block>\n";
3002 $allowed[$allow] = 1;
3003 }
3004 if (statement_block_size($block) > 1) {
3005 #print "APW: ALLOWED: lines block<$block>\n";
3006 $allowed[$allow] = 1;
3007 }
3008 $allow++;
3009 }
3010 if ($seen) {
3011 my $sum_allowed = 0;
3012 foreach (@allowed) {
3013 $sum_allowed += $_;
3014 }
3015 if ($sum_allowed == 0) {
3016 WARN("BRACES",
3017 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3018 } elsif ($sum_allowed != $allow &&
3019 $seen != $allow) {
3020 CHK("BRACES",
3021 "braces {} should be used on all arms of this statement\n" . $herectx);
3022 }
3023 }
3024 }
3025 }
3026 if (!defined $suppress_ifbraces{$linenr - 1} &&
3027 $line =~ /\b(if|while|for|else)\b/) {
3028 my $allowed = 0;
3029
3030 # Check the pre-context.
3031 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3032 #print "APW: ALLOWED: pre<$1>\n";
3033 $allowed = 1;
3034 }
3035
3036 my ($level, $endln, @chunks) =
3037 ctx_statement_full($linenr, $realcnt, $-[0]);
3038
3039 # Check the condition.
3040 my ($cond, $block) = @{$chunks[0]};
3041 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3042 if (defined $cond) {
3043 substr($block, 0, length($cond), '');
3044 }
3045 if (statement_lines($cond) > 1) {
3046 #print "APW: ALLOWED: cond<$cond>\n";
3047 $allowed = 1;
3048 }
3049 if ($block =~/\b(?:if|for|while)\b/) {
3050 #print "APW: ALLOWED: block<$block>\n";
3051 $allowed = 1;
3052 }
3053 if (statement_block_size($block) > 1) {
3054 #print "APW: ALLOWED: lines block<$block>\n";
3055 $allowed = 1;
3056 }
3057 # Check the post-context.
3058 if (defined $chunks[1]) {
3059 my ($cond, $block) = @{$chunks[1]};
3060 if (defined $cond) {
3061 substr($block, 0, length($cond), '');
3062 }
3063 if ($block =~ /^\s*\{/) {
3064 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3065 $allowed = 1;
3066 }
3067 }
3068 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3069 my $herectx = $here . "\n";
3070 my $cnt = statement_rawlines($block);
3071
3072 for (my $n = 0; $n < $cnt; $n++) {
3073 $herectx .= raw_line($linenr, $n) . "\n";
3074 }
3075
3076 WARN("BRACES",
3077 "braces {} are not necessary for single statement blocks\n" . $herectx);
3078 }
3079 }
3080
3081 # no volatiles please
3082 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3083 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3084 WARN("VOLATILE",
3085 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3086 }
3087
3088 # warn about #if 0
3089 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3090 CHK("REDUNDANT_CODE",
3091 "if this code is redundant consider removing it\n" .
3092 $herecurr);
3093 }
3094
3095 # check for needless kfree() checks
3096 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3097 my $expr = $1;
3098 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3099 WARN("NEEDLESS_KFREE",
3100 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
3101 }
3102 }
3103 # check for needless usb_free_urb() checks
3104 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3105 my $expr = $1;
3106 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
3107 WARN("NEEDLESS_USB_FREE_URB",
3108 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
3109 }
3110 }
3111
3112 # prefer usleep_range over udelay
3113 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3114 # ignore udelay's < 10, however
3115 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3116 CHK("USLEEP_RANGE",
3117 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3118 }
3119 }
3120
3121 # warn about unexpectedly long msleep's
3122 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3123 if ($1 < 20) {
3124 WARN("MSLEEP",
3125 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3126 }
3127 }
3128
3129 # warn about #ifdefs in C files
3130 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3131 # print "#ifdef in C files should be avoided\n";
3132 # print "$herecurr";
3133 # $clean = 0;
3134 # }
3135
3136 # warn about spacing in #ifdefs
3137 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3138 ERROR("SPACING",
3139 "exactly one space required after that #$1\n" . $herecurr);
3140 }
3141
3142 # check for spinlock_t definitions without a comment.
3143 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3144 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3145 my $which = $1;
3146 if (!ctx_has_comment($first_line, $linenr)) {
3147 CHK("UNCOMMENTED_DEFINITION",
3148 "$1 definition without comment\n" . $herecurr);
3149 }
3150 }
3151 # check for memory barriers without a comment.
3152 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3153 if (!ctx_has_comment($first_line, $linenr)) {
3154 CHK("MEMORY_BARRIER",
3155 "memory barrier without comment\n" . $herecurr);
3156 }
3157 }
3158 # check of hardware specific defines
3159 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3160 CHK("ARCH_DEFINES",
3161 "architecture specific defines should be avoided\n" . $herecurr);
3162 }
3163
3164 # Check that the storage class is at the beginning of a declaration
3165 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3166 WARN("STORAGE_CLASS",
3167 "storage class should be at the beginning of the declaration\n" . $herecurr)
3168 }
3169
3170 # check the location of the inline attribute, that it is between
3171 # storage class and type.
3172 if ($line =~ /\b$Type\s+$Inline\b/ ||
3173 $line =~ /\b$Inline\s+$Storage\b/) {
3174 ERROR("INLINE_LOCATION",
3175 "inline keyword should sit between storage class and type\n" . $herecurr);
3176 }
3177
3178 # Check for __inline__ and __inline, prefer inline
3179 if ($line =~ /\b(__inline__|__inline)\b/) {
3180 WARN("INLINE",
3181 "plain inline is preferred over $1\n" . $herecurr);
3182 }
3183
3184 # Check for __attribute__ packed, prefer __packed
3185 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3186 WARN("PREFER_PACKED",
3187 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3188 }
3189
3190 # Check for __attribute__ aligned, prefer __aligned
3191 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3192 WARN("PREFER_ALIGNED",
3193 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3194 }
3195
3196 # Check for __attribute__ format(printf, prefer __printf
3197 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3198 WARN("PREFER_PRINTF",
3199 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3200 }
3201
3202 # Check for __attribute__ format(scanf, prefer __scanf
3203 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3204 WARN("PREFER_SCANF",
3205 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3206 }
3207
3208 # check for sizeof(&)
3209 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3210 WARN("SIZEOF_ADDRESS",
3211 "sizeof(& should be avoided\n" . $herecurr);
3212 }
3213
3214 # check for line continuations in quoted strings with odd counts of "
3215 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3216 WARN("LINE_CONTINUATIONS",
3217 "Avoid line continuations in quoted strings\n" . $herecurr);
3218 }
3219
3220 # Check for misused memsets
3221 if ($^V && $^V ge 5.10.0 &&
3222 defined $stat &&
3223 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3224
3225 my $ms_addr = $2;
3226 my $ms_val = $7;
3227 my $ms_size = $12;
3228
3229 if ($ms_size =~ /^(0x|)0$/i) {
3230 ERROR("MEMSET",
3231 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3232 } elsif ($ms_size =~ /^(0x|)1$/i) {
3233 WARN("MEMSET",
3234 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3235 }
3236 }
3237
3238 # typecasts on min/max could be min_t/max_t
3239 if ($^V && $^V ge 5.10.0 &&
3240 defined $stat &&
3241 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3242 if (defined $2 || defined $7) {
3243 my $call = $1;
3244 my $cast1 = deparenthesize($2);
3245 my $arg1 = $3;
3246 my $cast2 = deparenthesize($7);
3247 my $arg2 = $8;
3248 my $cast;
3249
3250 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3251 $cast = "$cast1 or $cast2";
3252 } elsif ($cast1 ne "") {
3253 $cast = $cast1;
3254 } else {
3255 $cast = $cast2;
3256 }
3257 WARN("MINMAX",
3258 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3259 }
3260 }
3261
3262 # check for new externs in .c files.
3263 if ($realfile =~ /\.c$/ && defined $stat &&
3264 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3265 {
3266 my $function_name = $1;
3267 my $paren_space = $2;
3268
3269 my $s = $stat;
3270 if (defined $cond) {
3271 substr($s, 0, length($cond), '');
3272 }
3273 if ($s =~ /^\s*;/ &&
3274 $function_name ne 'uninitialized_var')
3275 {
3276 WARN("AVOID_EXTERNS",
3277 "externs should be avoided in .c files\n" . $herecurr);
3278 }
3279
3280 if ($paren_space =~ /\n/) {
3281 WARN("FUNCTION_ARGUMENTS",
3282 "arguments for function declarations should follow identifier\n" . $herecurr);
3283 }
3284
3285 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3286 $stat =~ /^.\s*extern\s+/)
3287 {
3288 WARN("AVOID_EXTERNS",
3289 "externs should be avoided in .c files\n" . $herecurr);
3290 }
3291
3292 # checks for new __setup's
3293 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3294 my $name = $1;
3295
3296 if (!grep(/$name/, @setup_docs)) {
3297 CHK("UNDOCUMENTED_SETUP",
3298 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3299 }
3300 }
3301
3302 # check for pointless casting of kmalloc return
3303 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3304 WARN("UNNECESSARY_CASTS",
3305 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3306 }
3307
3308 # check for multiple semicolons
3309 if ($line =~ /;\s*;\s*$/) {
3310 WARN("ONE_SEMICOLON",
3311 "Statements terminations use 1 semicolon\n" . $herecurr);
3312 }
3313
3314 # check for gcc specific __FUNCTION__
3315 if ($line =~ /__FUNCTION__/) {
3316 WARN("USE_FUNC",
3317 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3318 }
3319
3320 # check for use of yield()
3321 if ($line =~ /\byield\s*\(\s*\)/) {
3322 WARN("YIELD",
3323 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3324 }
3325
3326 # check for semaphores initialized locked
3327 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3328 WARN("CONSIDER_COMPLETION",
3329 "consider using a completion\n" . $herecurr);
3330 }
3331
3332 # recommend kstrto* over simple_strto* and strict_strto*
3333 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3334 WARN("CONSIDER_KSTRTO",
3335 "$1 is obsolete, use k$3 instead\n" . $herecurr);
3336 }
3337
3338 # check for __initcall(), use device_initcall() explicitly please
3339 if ($line =~ /^.\s*__initcall\s*\(/) {
3340 WARN("USE_DEVICE_INITCALL",
3341 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3342 }
3343
3344 # check for various ops structs, ensure they are const.
3345 my $struct_ops = qr{acpi_dock_ops|
3346 address_space_operations|
3347 backlight_ops|
3348 block_device_operations|
3349 dentry_operations|
3350 dev_pm_ops|
3351 dma_map_ops|
3352 extent_io_ops|
3353 file_lock_operations|
3354 file_operations|
3355 hv_ops|
3356 ide_dma_ops|
3357 intel_dvo_dev_ops|
3358 item_operations|
3359 iwl_ops|
3360 kgdb_arch|
3361 kgdb_io|
3362 kset_uevent_ops|
3363 lock_manager_operations|
3364 microcode_ops|
3365 mtrr_ops|
3366 neigh_ops|
3367 nlmsvc_binding|
3368 pci_raw_ops|
3369 pipe_buf_operations|
3370 platform_hibernation_ops|
3371 platform_suspend_ops|
3372 proto_ops|
3373 rpc_pipe_ops|
3374 seq_operations|
3375 snd_ac97_build_ops|
3376 soc_pcmcia_socket_ops|
3377 stacktrace_ops|
3378 sysfs_ops|
3379 tty_operations|
3380 usb_mon_operations|
3381 wd_ops}x;
3382 if ($line !~ /\bconst\b/ &&
3383 $line =~ /\bstruct\s+($struct_ops)\b/) {
3384 WARN("CONST_STRUCT",
3385 "struct $1 should normally be const\n" .
3386 $herecurr);
3387 }
3388
3389 # use of NR_CPUS is usually wrong
3390 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3391 if ($line =~ /\bNR_CPUS\b/ &&
3392 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3393 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3394 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3395 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3396 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3397 {
3398 WARN("NR_CPUS",
3399 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3400 }
3401
3402 # check for %L{u,d,i} in strings
3403 my $string;
3404 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3405 $string = substr($rawline, $-[1], $+[1] - $-[1]);
3406 $string =~ s/%%/__/g;
3407 if ($string =~ /(?<!%)%L[udi]/) {
3408 WARN("PRINTF_L",
3409 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3410 last;
3411 }
3412 }
3413
3414 # whine mightly about in_atomic
3415 if ($line =~ /\bin_atomic\s*\(/) {
3416 if ($realfile =~ m@^drivers/@) {
3417 ERROR("IN_ATOMIC",
3418 "do not use in_atomic in drivers\n" . $herecurr);
3419 } elsif ($realfile !~ m@^kernel/@) {
3420 WARN("IN_ATOMIC",
3421 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3422 }
3423 }
3424
3425 # check for lockdep_set_novalidate_class
3426 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3427 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3428 if ($realfile !~ m@^kernel/lockdep@ &&
3429 $realfile !~ m@^include/linux/lockdep@ &&
3430 $realfile !~ m@^drivers/base/core@) {
3431 ERROR("LOCKDEP",
3432 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3433 }
3434 }
3435
3436 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3437 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3438 WARN("EXPORTED_WORLD_WRITABLE",
3439 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3440 }
3441 }
3442
3443 # If we have no input at all, then there is nothing to report on
3444 # so just keep quiet.
3445 if ($#rawlines == -1) {
3446 exit(0);
3447 }
3448
3449 # In mailback mode only produce a report in the negative, for
3450 # things that appear to be patches.
3451 if ($mailback && ($clean == 1 || !$is_patch)) {
3452 exit(0);
3453 }
3454
3455 # This is not a patch, and we are are in 'no-patch' mode so
3456 # just keep quiet.
3457 if (!$chk_patch && !$is_patch) {
3458 exit(0);
3459 }
3460
3461 if (!$is_patch) {
3462 ERROR("NOT_UNIFIED_DIFF",
3463 "Does not appear to be a unified-diff format patch\n");
3464 }
3465 if ($is_patch && $chk_signoff && $signoff == 0) {
3466 ERROR("MISSING_SIGN_OFF",
3467 "Missing Signed-off-by: line(s)\n");
3468 }
3469
3470 print report_dump();
3471 if ($summary && !($clean == 1 && $quiet == 1)) {
3472 print "$filename " if ($summary_file);
3473 print "total: $cnt_error errors, $cnt_warn warnings, " .
3474 (($check)? "$cnt_chk checks, " : "") .
3475 "$cnt_lines lines checked\n";
3476 print "\n" if ($quiet == 0);
3477 }
3478
3479 if ($quiet == 0) {
3480
3481 if ($^V lt 5.10.0) {
3482 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3483 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3484 }
3485
3486 # If there were whitespace errors which cleanpatch can fix
3487 # then suggest that.
3488 if ($rpt_cleaners) {
3489 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3490 print " scripts/cleanfile\n\n";
3491 $rpt_cleaners = 0;
3492 }
3493 }
3494
3495 if ($quiet == 0 && keys %ignore_type) {
3496 print "NOTE: Ignored message types:";
3497 foreach my $ignore (sort keys %ignore_type) {
3498 print " $ignore";
3499 }
3500 print "\n\n";
3501 }
3502
3503 if ($clean == 1 && $quiet == 0) {
3504 print "$vname has no obvious style problems and is ready for submission.\n"
3505 }
3506 if ($clean == 0 && $quiet == 0) {
3507 print << "EOM";
3508 $vname has style problems, please review.
3509
3510 If any of these errors are false positives, please report
3511 them to the maintainer, see CHECKPATCH in MAINTAINERS.
3512 EOM
3513 }
3514
3515 return $clean;
3516 }