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

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)