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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)