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