tcl: add ASUS RT-N66U config
[openocd.git] / tools / scripts / checkpatch.pl
blob7a30ae7e3fbeb42294950a7cdfb982d1b8d717eb
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
8 use strict;
10 my $P = $0;
11 $P =~ s@.*/@@g;
13 my $V = '0.32';
15 use Getopt::Long qw(:config no_auto_abbrev);
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";
37 sub help {
38 my ($exitcode) = @_;
40 print << "EOM";
41 Usage: $P [OPTION]... [FILE]...
42 Version: $V
44 Options:
45 -q, --quiet quiet
46 --no-tree run without a kernel tree
47 --no-signoff do not check for 'Signed-off-by' line
48 --patch treat FILE as patchfile (default)
49 --emacs emacs compile window format
50 --terse one line per report
51 -f, --file treat FILE as regular source file
52 --subjective, --strict enable more subjective tests
53 --ignore TYPE(,TYPE2...) ignore various comma separated message types
54 --show-types show the message "types" in the output
55 --root=PATH PATH to the kernel tree root
56 --no-summary suppress the per-file summary
57 --mailback only produce a report in case of warnings/errors
58 --summary-file include the filename in summary
59 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
60 'values', 'possible', 'type', and 'attr' (default
61 is all off)
62 --test-only=WORD report only warnings/errors containing WORD
63 literally
64 -h, --help, --version display this help and exit
66 When FILE is - read standard input.
67 EOM
69 exit($exitcode);
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";
78 while (<$conffile>) {
79 my $line = $_;
81 $line =~ s/\s*\n?$//g;
82 $line =~ s/^\s*//g;
83 $line =~ s/\s+/ /g;
85 next if ($line =~ m/^\s*#/);
86 next if ($line =~ m/^\s*$/);
88 my @words = split(" ", $line);
89 foreach my $word (@words) {
90 last if ($word =~ m/^#/);
91 push (@conf_args, $word);
94 close($conffile);
95 unshift(@ARGV, @conf_args) if @conf_args;
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,
115 'debug=s' => \%debug,
116 'test-only=s' => \$tst_only,
117 'h|help' => \$help,
118 'version' => \$help
119 ) or help(1);
121 help(0) if ($help);
123 my $exit = 0;
125 if ($#ARGV < 0) {
126 print "$P: no input files\n";
127 exit(1);
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]/;
137 next if ($word =~ m/^\s*#/);
138 next if ($word =~ m/^\s*$/);
140 $ignore_type{$word}++;
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 ($@);
153 my $rpt_cleaners = 0;
155 if ($terse) {
156 $emacs = 1;
157 $quiet++;
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";
165 } else {
166 if (top_of_kernel_tree('.')) {
167 $root = '.';
168 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
169 top_of_kernel_tree($1)) {
170 $root = $1;
174 if (!defined $root) {
175 print "Must be run from the top-level dir. of a kernel tree\n";
176 exit(2);
180 my $emitted_corrupt = 0;
182 our $Ident = qr{
183 [A-Za-z_][A-Za-z\d_]*
184 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
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
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
225 our $Modifier;
226 our $Inline = qr{inline|__always_inline|noinline};
227 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
228 our $Lval = qr{$Ident(?:$Member)*};
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 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
239 our $NonptrType;
240 our $Type;
241 our $Declare;
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
254 our $typeTypedefs = qr{(?x:
255 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
256 atomic_t
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)+
268 our $signature_tags = qr{(?xi:
269 Signed-off-by:|
270 Acked-by:|
271 Tested-by:|
272 Reviewed-by:|
273 Reported-by:|
274 To:|
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},
298 our @modifierList = (
299 qr{fastcall},
302 our $allowed_asm_includes = qr{(?x:
303 irq|
304 memory
306 # memory.h: ARM has a custom one
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+)*
315 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
316 (?:$typeTypedefs\b)|
317 (?:${all}\b)
319 (?:\s+$Modifier|\s+const)*
321 $Type = qr{
322 $NonptrType
323 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
324 (?:\s+$Inline|\s+$Modifier)*
326 $Declare = qr{(?:$Storage\s+)?$Type};
328 build_types();
330 our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
332 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
333 our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
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;
344 $chk_signoff = 0 if ($file);
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);
358 } elsif ($entry !~ m@/@) {
359 push(@dep_functions, $entry);
364 close($REMOVE);
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";
381 if ($filename eq '-') {
382 $vname = 'Your patch';
383 } else {
384 $vname = $filename;
386 while (<$FILE>) {
387 chomp;
388 push(@rawlines, $_);
390 close($FILE);
391 if (!process($filename)) {
392 $exit = 1;
394 @rawlines = ();
395 @lines = ();
398 exit($exit);
400 sub top_of_kernel_tree {
401 my ($root) = @_;
403 my @tree_check = (
404 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
405 "README", "Documentation", "arch", "include", "drivers",
406 "fs", "init", "ipc", "kernel", "lib", "scripts",
409 foreach my $check (@tree_check) {
410 if (! -e $root . '/' . $check) {
411 return 0;
414 return 1;
417 sub parse_email {
418 my ($formatted_email) = @_;
420 my $name = "";
421 my $address = "";
422 my $comment = "";
424 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
425 $name = $1;
426 $address = $2;
427 $comment = $3 if defined $3;
428 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
429 $address = $1;
430 $comment = $2 if defined $2;
431 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
432 $address = $1;
433 $comment = $2 if defined $2;
434 $formatted_email =~ s/$address.*$//;
435 $name = $formatted_email;
436 $name =~ s/^\s+|\s+$//g;
437 $name =~ s/^\"|\"$//g;
438 # If there's a name left after stripping spaces and
439 # leading quotes, and the address doesn't have both
440 # leading and trailing angle brackets, the address
441 # is invalid. ie:
442 # "joe smith joe@smith.com" bad
443 # "joe smith <joe@smith.com" bad
444 if ($name ne "" && $address !~ /^<[^>]+>$/) {
445 $name = "";
446 $address = "";
447 $comment = "";
449 } elsif ($formatted_email eq "jenkins") {
450 $address = "jenkins"
453 $name =~ s/^\s+|\s+$//g;
454 $name =~ s/^\"|\"$//g;
455 $address =~ s/^\s+|\s+$//g;
456 $address =~ s/^\<|\>$//g;
458 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
459 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
460 $name = "\"$name\"";
463 return ($name, $address, $comment);
466 sub format_email {
467 my ($name, $address) = @_;
469 my $formatted_email;
471 $name =~ s/^\s+|\s+$//g;
472 $name =~ s/^\"|\"$//g;
473 $address =~ s/^\s+|\s+$//g;
475 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
476 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
477 $name = "\"$name\"";
480 if ("$name" eq "") {
481 $formatted_email = "$address";
482 } else {
483 $formatted_email = "$name <$address>";
486 return $formatted_email;
489 sub which_conf {
490 my ($conf) = @_;
492 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
493 if (-e "$path/$conf") {
494 return "$path/$conf";
498 return "";
501 sub expand_tabs {
502 my ($str) = @_;
504 my $res = '';
505 my $n = 0;
506 for my $c (split(//, $str)) {
507 if ($c eq "\t") {
508 $res .= ' ';
509 $n++;
510 for (; ($n % 4) != 0; $n++) {
511 $res .= ' ';
513 next;
515 $res .= $c;
516 $n++;
519 return $res;
521 sub copy_spacing {
522 (my $res = shift) =~ tr/\t/ /c;
523 return $res;
526 sub line_stats {
527 my ($line) = @_;
529 # Drop the diff line leader and expand tabs
530 $line =~ s/^.//;
531 $line = expand_tabs($line);
533 # Pick the indent from the front of the line.
534 my ($white) = ($line =~ /^(\s*)/);
536 return (length($line), length($white));
539 my $sanitise_quote = '';
541 sub sanitise_line_reset {
542 my ($in_comment) = @_;
544 if ($in_comment) {
545 $sanitise_quote = '*/';
546 } else {
547 $sanitise_quote = '';
550 sub sanitise_line {
551 my ($line) = @_;
553 my $res = '';
554 my $l = '';
556 my $qlen = 0;
557 my $off = 0;
558 my $c;
560 # Always copy over the diff marker.
561 $res = substr($line, 0, 1);
563 for ($off = 1; $off < length($line); $off++) {
564 $c = substr($line, $off, 1);
566 # Comments we are wacking completly including the begin
567 # and end, all to $;.
568 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
569 $sanitise_quote = '*/';
571 substr($res, $off, 2, "$;$;");
572 $off++;
573 next;
575 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
576 $sanitise_quote = '';
577 substr($res, $off, 2, "$;$;");
578 $off++;
579 next;
581 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
582 $sanitise_quote = '//';
584 substr($res, $off, 2, $sanitise_quote);
585 $off++;
586 next;
589 # A \ in a string means ignore the next character.
590 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
591 $c eq "\\") {
592 substr($res, $off, 2, 'XX');
593 $off++;
594 next;
596 # Regular quotes.
597 if ($c eq "'" || $c eq '"') {
598 if ($sanitise_quote eq '') {
599 $sanitise_quote = $c;
601 substr($res, $off, 1, $c);
602 next;
603 } elsif ($sanitise_quote eq $c) {
604 $sanitise_quote = '';
608 #print "c<$c> SQ<$sanitise_quote>\n";
609 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
610 substr($res, $off, 1, $;);
611 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
612 substr($res, $off, 1, $;);
613 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
614 substr($res, $off, 1, 'X');
615 } else {
616 substr($res, $off, 1, $c);
620 if ($sanitise_quote eq '//') {
621 $sanitise_quote = '';
624 # The pathname on a #include may be surrounded by '<' and '>'.
625 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
626 my $clean = 'X' x length($1);
627 $res =~ s@\<.*\>@<$clean>@;
629 # The whole of a #error is a string.
630 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
631 my $clean = 'X' x length($1);
632 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
635 return $res;
638 sub ctx_statement_block {
639 my ($linenr, $remain, $off) = @_;
640 my $line = $linenr - 1;
641 my $blk = '';
642 my $soff = $off;
643 my $coff = $off - 1;
644 my $coff_set = 0;
646 my $loff = 0;
648 my $type = '';
649 my $level = 0;
650 my @stack = ();
651 my $p;
652 my $c;
653 my $len = 0;
655 my $remainder;
656 while (1) {
657 @stack = (['', 0]) if ($#stack == -1);
659 #warn "CSB: blk<$blk> remain<$remain>\n";
660 # If we are about to drop off the end, pull in more
661 # context.
662 if ($off >= $len) {
663 for (; $remain > 0; $line++) {
664 last if (!defined $lines[$line]);
665 next if ($lines[$line] =~ /^-/);
666 $remain--;
667 $loff = $len;
668 $blk .= $lines[$line] . "\n";
669 $len = length($blk);
670 $line++;
671 last;
673 # Bail if there is no further context.
674 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
675 if ($off >= $len) {
676 last;
679 $p = $c;
680 $c = substr($blk, $off, 1);
681 $remainder = substr($blk, $off);
683 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
685 # Handle nested #if/#else.
686 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
687 push(@stack, [ $type, $level ]);
688 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
689 ($type, $level) = @{$stack[$#stack - 1]};
690 } elsif ($remainder =~ /^#\s*endif\b/) {
691 ($type, $level) = @{pop(@stack)};
694 # Statement ends at the ';' or a close '}' at the
695 # outermost level.
696 if ($level == 0 && $c eq ';') {
697 last;
700 # An else is really a conditional as long as its not else if
701 if ($level == 0 && $coff_set == 0 &&
702 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
703 $remainder =~ /^(else)(?:\s|{)/ &&
704 $remainder !~ /^else\s+if\b/) {
705 $coff = $off + length($1) - 1;
706 $coff_set = 1;
707 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
708 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
711 if (($type eq '' || $type eq '(') && $c eq '(') {
712 $level++;
713 $type = '(';
715 if ($type eq '(' && $c eq ')') {
716 $level--;
717 $type = ($level != 0)? '(' : '';
719 if ($level == 0 && $coff < $soff) {
720 $coff = $off;
721 $coff_set = 1;
722 #warn "CSB: mark coff<$coff>\n";
725 if (($type eq '' || $type eq '{') && $c eq '{') {
726 $level++;
727 $type = '{';
729 if ($type eq '{' && $c eq '}') {
730 $level--;
731 $type = ($level != 0)? '{' : '';
733 if ($level == 0) {
734 if (substr($blk, $off + 1, 1) eq ';') {
735 $off++;
737 last;
740 $off++;
742 # We are truly at the end, so shuffle to the next line.
743 if ($off == $len) {
744 $loff = $len + 1;
745 $line++;
746 $remain--;
749 my $statement = substr($blk, $soff, $off - $soff + 1);
750 my $condition = substr($blk, $soff, $coff - $soff + 1);
752 #warn "STATEMENT<$statement>\n";
753 #warn "CONDITION<$condition>\n";
755 #print "coff<$coff> soff<$off> loff<$loff>\n";
757 return ($statement, $condition,
758 $line, $remain + 1, $off - $loff + 1, $level);
761 sub statement_lines {
762 my ($stmt) = @_;
764 # Strip the diff line prefixes and rip blank lines at start and end.
765 $stmt =~ s/(^|\n)./$1/g;
766 $stmt =~ s/^\s*//;
767 $stmt =~ s/\s*$//;
769 my @stmt_lines = ($stmt =~ /\n/g);
771 return $#stmt_lines + 2;
774 sub statement_rawlines {
775 my ($stmt) = @_;
777 my @stmt_lines = ($stmt =~ /\n/g);
779 return $#stmt_lines + 2;
782 sub statement_block_size {
783 my ($stmt) = @_;
785 $stmt =~ s/(^|\n)./$1/g;
786 $stmt =~ s/^\s*{//;
787 $stmt =~ s/}\s*$//;
788 $stmt =~ s/^\s*//;
789 $stmt =~ s/\s*$//;
791 my @stmt_lines = ($stmt =~ /\n/g);
792 my @stmt_statements = ($stmt =~ /;/g);
794 my $stmt_lines = $#stmt_lines + 2;
795 my $stmt_statements = $#stmt_statements + 1;
797 if ($stmt_lines > $stmt_statements) {
798 return $stmt_lines;
799 } else {
800 return $stmt_statements;
804 sub ctx_statement_full {
805 my ($linenr, $remain, $off) = @_;
806 my ($statement, $condition, $level);
808 my (@chunks);
810 # Grab the first conditional/block pair.
811 ($statement, $condition, $linenr, $remain, $off, $level) =
812 ctx_statement_block($linenr, $remain, $off);
813 #print "F: c<$condition> s<$statement> remain<$remain>\n";
814 push(@chunks, [ $condition, $statement ]);
815 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
816 return ($level, $linenr, @chunks);
819 # Pull in the following conditional/block pairs and see if they
820 # could continue the statement.
821 for (;;) {
822 ($statement, $condition, $linenr, $remain, $off, $level) =
823 ctx_statement_block($linenr, $remain, $off);
824 #print "C: c<$condition> s<$statement> remain<$remain>\n";
825 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
826 #print "C: push\n";
827 push(@chunks, [ $condition, $statement ]);
830 return ($level, $linenr, @chunks);
833 sub ctx_block_get {
834 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
835 my $line;
836 my $start = $linenr - 1;
837 my $blk = '';
838 my @o;
839 my @c;
840 my @res = ();
842 my $level = 0;
843 my @stack = ($level);
844 for ($line = $start; $remain > 0; $line++) {
845 next if ($rawlines[$line] =~ /^-/);
846 $remain--;
848 $blk .= $rawlines[$line];
850 # Handle nested #if/#else.
851 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
852 push(@stack, $level);
853 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
854 $level = $stack[$#stack - 1];
855 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
856 $level = pop(@stack);
859 foreach my $c (split(//, $lines[$line])) {
860 ##print "C<$c>L<$level><$open$close>O<$off>\n";
861 if ($off > 0) {
862 $off--;
863 next;
866 if ($c eq $close && $level > 0) {
867 $level--;
868 last if ($level == 0);
869 } elsif ($c eq $open) {
870 $level++;
874 if (!$outer || $level <= 1) {
875 push(@res, $rawlines[$line]);
878 last if ($level == 0);
881 return ($level, @res);
883 sub ctx_block_outer {
884 my ($linenr, $remain) = @_;
886 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
887 return @r;
889 sub ctx_block {
890 my ($linenr, $remain) = @_;
892 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
893 return @r;
895 sub ctx_statement {
896 my ($linenr, $remain, $off) = @_;
898 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
899 return @r;
901 sub ctx_block_level {
902 my ($linenr, $remain) = @_;
904 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
906 sub ctx_statement_level {
907 my ($linenr, $remain, $off) = @_;
909 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
912 sub ctx_locate_comment {
913 my ($first_line, $end_line) = @_;
915 # Catch a comment on the end of the line itself.
916 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
917 return $current_comment if (defined $current_comment);
919 # Look through the context and try and figure out if there is a
920 # comment.
921 my $in_comment = 0;
922 $current_comment = '';
923 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
924 my $line = $rawlines[$linenr - 1];
925 #warn " $line\n";
926 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
927 $in_comment = 1;
929 if ($line =~ m@/\*@) {
930 $in_comment = 1;
932 if (!$in_comment && $current_comment ne '') {
933 $current_comment = '';
935 $current_comment .= $line . "\n" if ($in_comment);
936 if ($line =~ m@\*/@) {
937 $in_comment = 0;
941 chomp($current_comment);
942 return($current_comment);
944 sub ctx_has_comment {
945 my ($first_line, $end_line) = @_;
946 my $cmt = ctx_locate_comment($first_line, $end_line);
948 ##print "LINE: $rawlines[$end_line - 1 ]\n";
949 ##print "CMMT: $cmt\n";
951 return ($cmt ne '');
954 sub raw_line {
955 my ($linenr, $cnt) = @_;
957 my $offset = $linenr - 1;
958 $cnt++;
960 my $line;
961 while ($cnt) {
962 $line = $rawlines[$offset++];
963 next if (defined($line) && $line =~ /^-/);
964 $cnt--;
967 return $line;
970 sub cat_vet {
971 my ($vet) = @_;
972 my ($res, $coded);
974 $res = '';
975 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
976 $res .= $1;
977 if ($2 ne '') {
978 $coded = sprintf("^%c", unpack('C', $2) + 64);
979 $res .= $coded;
982 $res =~ s/$/\$/;
984 return $res;
987 my $av_preprocessor = 0;
988 my $av_pending;
989 my @av_paren_type;
990 my $av_pend_colon;
992 sub annotate_reset {
993 $av_preprocessor = 0;
994 $av_pending = '_';
995 @av_paren_type = ('E');
996 $av_pend_colon = 'O';
999 sub annotate_values {
1000 my ($stream, $type) = @_;
1002 my $res;
1003 my $var = '_' x length($stream);
1004 my $cur = $stream;
1006 print "$stream\n" if ($dbg_values > 1);
1008 while (length($cur)) {
1009 @av_paren_type = ('E') if ($#av_paren_type < 0);
1010 print " <" . join('', @av_paren_type) .
1011 "> <$type> <$av_pending>" if ($dbg_values > 1);
1012 if ($cur =~ /^(\s+)/o) {
1013 print "WS($1)\n" if ($dbg_values > 1);
1014 if ($1 =~ /\n/ && $av_preprocessor) {
1015 $type = pop(@av_paren_type);
1016 $av_preprocessor = 0;
1019 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1020 print "CAST($1)\n" if ($dbg_values > 1);
1021 push(@av_paren_type, $type);
1022 $type = 'C';
1024 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1025 print "DECLARE($1)\n" if ($dbg_values > 1);
1026 $type = 'T';
1028 } elsif ($cur =~ /^($Modifier)\s*/) {
1029 print "MODIFIER($1)\n" if ($dbg_values > 1);
1030 $type = 'T';
1032 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1033 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1034 $av_preprocessor = 1;
1035 push(@av_paren_type, $type);
1036 if ($2 ne '') {
1037 $av_pending = 'N';
1039 $type = 'E';
1041 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1042 print "UNDEF($1)\n" if ($dbg_values > 1);
1043 $av_preprocessor = 1;
1044 push(@av_paren_type, $type);
1046 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1047 print "PRE_START($1)\n" if ($dbg_values > 1);
1048 $av_preprocessor = 1;
1050 push(@av_paren_type, $type);
1051 push(@av_paren_type, $type);
1052 $type = 'E';
1054 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1055 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1056 $av_preprocessor = 1;
1058 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1060 $type = 'E';
1062 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1063 print "PRE_END($1)\n" if ($dbg_values > 1);
1065 $av_preprocessor = 1;
1067 # Assume all arms of the conditional end as this
1068 # one does, and continue as if the #endif was not here.
1069 pop(@av_paren_type);
1070 push(@av_paren_type, $type);
1071 $type = 'E';
1073 } elsif ($cur =~ /^(\\\n)/o) {
1074 print "PRECONT($1)\n" if ($dbg_values > 1);
1076 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1077 print "ATTR($1)\n" if ($dbg_values > 1);
1078 $av_pending = $type;
1079 $type = 'N';
1081 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1082 print "SIZEOF($1)\n" if ($dbg_values > 1);
1083 if (defined $2) {
1084 $av_pending = 'V';
1086 $type = 'N';
1088 } elsif ($cur =~ /^(if|while|for)\b/o) {
1089 print "COND($1)\n" if ($dbg_values > 1);
1090 $av_pending = 'E';
1091 $type = 'N';
1093 } elsif ($cur =~/^(case)/o) {
1094 print "CASE($1)\n" if ($dbg_values > 1);
1095 $av_pend_colon = 'C';
1096 $type = 'N';
1098 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1099 print "KEYWORD($1)\n" if ($dbg_values > 1);
1100 $type = 'N';
1102 } elsif ($cur =~ /^(\()/o) {
1103 print "PAREN('$1')\n" if ($dbg_values > 1);
1104 push(@av_paren_type, $av_pending);
1105 $av_pending = '_';
1106 $type = 'N';
1108 } elsif ($cur =~ /^(\))/o) {
1109 my $new_type = pop(@av_paren_type);
1110 if ($new_type ne '_') {
1111 $type = $new_type;
1112 print "PAREN('$1') -> $type\n"
1113 if ($dbg_values > 1);
1114 } else {
1115 print "PAREN('$1')\n" if ($dbg_values > 1);
1118 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1119 print "FUNC($1)\n" if ($dbg_values > 1);
1120 $type = 'V';
1121 $av_pending = 'V';
1123 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1124 if (defined $2 && $type eq 'C' || $type eq 'T') {
1125 $av_pend_colon = 'B';
1126 } elsif ($type eq 'E') {
1127 $av_pend_colon = 'L';
1129 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1130 $type = 'V';
1132 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1133 print "IDENT($1)\n" if ($dbg_values > 1);
1134 $type = 'V';
1136 } elsif ($cur =~ /^($Assignment)/o) {
1137 print "ASSIGN($1)\n" if ($dbg_values > 1);
1138 $type = 'N';
1140 } elsif ($cur =~/^(;|{|})/) {
1141 print "END($1)\n" if ($dbg_values > 1);
1142 $type = 'E';
1143 $av_pend_colon = 'O';
1145 } elsif ($cur =~/^(,)/) {
1146 print "COMMA($1)\n" if ($dbg_values > 1);
1147 $type = 'C';
1149 } elsif ($cur =~ /^(\?)/o) {
1150 print "QUESTION($1)\n" if ($dbg_values > 1);
1151 $type = 'N';
1153 } elsif ($cur =~ /^(:)/o) {
1154 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1156 substr($var, length($res), 1, $av_pend_colon);
1157 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1158 $type = 'E';
1159 } else {
1160 $type = 'N';
1162 $av_pend_colon = 'O';
1164 } elsif ($cur =~ /^(\[)/o) {
1165 print "CLOSE($1)\n" if ($dbg_values > 1);
1166 $type = 'N';
1168 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1169 my $variant;
1171 print "OPV($1)\n" if ($dbg_values > 1);
1172 if ($type eq 'V') {
1173 $variant = 'B';
1174 } else {
1175 $variant = 'U';
1178 substr($var, length($res), 1, $variant);
1179 $type = 'N';
1181 } elsif ($cur =~ /^($Operators)/o) {
1182 print "OP($1)\n" if ($dbg_values > 1);
1183 if ($1 ne '++' && $1 ne '--') {
1184 $type = 'N';
1187 } elsif ($cur =~ /(^.)/o) {
1188 print "C($1)\n" if ($dbg_values > 1);
1190 if (defined $1) {
1191 $cur = substr($cur, length($1));
1192 $res .= $type x length($1);
1196 return ($res, $var);
1199 sub possible {
1200 my ($possible, $line) = @_;
1201 my $notPermitted = qr{(?:
1202 ^(?:
1203 $Modifier|
1204 $Storage|
1205 $Type|
1206 DEFINE_\S+
1208 ^(?:
1209 goto|
1210 return|
1211 case|
1212 else|
1213 asm|__asm__|
1215 )(?:\s|$)|
1216 ^(?:typedef|struct|enum)\b
1217 )}x;
1218 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1219 if ($possible !~ $notPermitted) {
1220 # Check for modifiers.
1221 $possible =~ s/\s*$Storage\s*//g;
1222 $possible =~ s/\s*$Sparse\s*//g;
1223 if ($possible =~ /^\s*$/) {
1225 } elsif ($possible =~ /\s/) {
1226 $possible =~ s/\s*$Type\s*//g;
1227 for my $modifier (split(' ', $possible)) {
1228 if ($modifier !~ $notPermitted) {
1229 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1230 push(@modifierList, $modifier);
1234 } else {
1235 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1236 push(@typeList, $possible);
1238 build_types();
1239 } else {
1240 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1244 my $prefix = '';
1246 sub show_type {
1247 return !defined $ignore_type{$_[0]};
1250 sub report {
1251 if (!show_type($_[1]) ||
1252 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1253 return 0;
1255 my $line;
1256 if ($show_types) {
1257 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1258 } else {
1259 $line = "$prefix$_[0]: $_[2]\n";
1261 $line = (split('\n', $line))[0] . "\n" if ($terse);
1263 push(our @report, $line);
1265 return 1;
1267 sub report_dump {
1268 our @report;
1271 sub ERROR {
1272 if (report("ERROR", $_[0], $_[1])) {
1273 our $clean = 0;
1274 our $cnt_error++;
1277 sub WARN {
1278 if (report("WARNING", $_[0], $_[1])) {
1279 our $clean = 0;
1280 our $cnt_warn++;
1283 sub CHK {
1284 if ($check && report("CHECK", $_[0], $_[1])) {
1285 our $clean = 0;
1286 our $cnt_chk++;
1290 sub check_absolute_file {
1291 my ($absolute, $herecurr) = @_;
1292 my $file = $absolute;
1294 ##print "absolute<$absolute>\n";
1296 # See if any suffix of this path is a path within the tree.
1297 while ($file =~ s@^[^/]*/@@) {
1298 if (-f "$root/$file") {
1299 ##print "file<$file>\n";
1300 last;
1303 if (! -f _) {
1304 return 0;
1307 # It is, so see if the prefix is acceptable.
1308 my $prefix = $absolute;
1309 substr($prefix, -length($file)) = '';
1311 ##print "prefix<$prefix>\n";
1312 if ($prefix ne ".../") {
1313 WARN("USE_RELATIVE_PATH",
1314 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1318 sub process {
1319 my $filename = shift;
1321 my $linenr=0;
1322 my $prevline="";
1323 my $prevrawline="";
1324 my $stashline="";
1325 my $stashrawline="";
1327 my $length;
1328 my $indent;
1329 my $previndent=0;
1330 my $stashindent=0;
1332 our $clean = 1;
1333 my $signoff = 0;
1334 my $is_patch = 0;
1336 our @report = ();
1337 our $cnt_lines = 0;
1338 our $cnt_error = 0;
1339 our $cnt_warn = 0;
1340 our $cnt_chk = 0;
1342 # Trace the real file/line as we go.
1343 my $realfile = '';
1344 my $realline = 0;
1345 my $realcnt = 0;
1346 my $here = '';
1347 my $in_comment = 0;
1348 my $comment_edge = 0;
1349 my $first_line = 0;
1350 my $p1_prefix = '';
1352 my $prev_values = 'E';
1354 # suppression flags
1355 my %suppress_ifbraces;
1356 my %suppress_whiletrailers;
1357 my %suppress_export;
1359 # Pre-scan the patch sanitizing the lines.
1360 # Pre-scan the patch looking for any __setup documentation.
1362 my @setup_docs = ();
1363 my $setup_docs = 0;
1365 sanitise_line_reset();
1366 my $line;
1367 foreach my $rawline (@rawlines) {
1368 $linenr++;
1369 $line = $rawline;
1371 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1372 $setup_docs = 0;
1373 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1374 $setup_docs = 1;
1376 #next;
1378 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1379 $realline=$1-1;
1380 if (defined $2) {
1381 $realcnt=$3+1;
1382 } else {
1383 $realcnt=1+1;
1385 $in_comment = 0;
1387 # Guestimate if this is a continuing comment. Run
1388 # the context looking for a comment "edge". If this
1389 # edge is a close comment then we must be in a comment
1390 # at context start.
1391 my $edge;
1392 my $cnt = $realcnt;
1393 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1394 next if (defined $rawlines[$ln - 1] &&
1395 $rawlines[$ln - 1] =~ /^-/);
1396 $cnt--;
1397 #print "RAW<$rawlines[$ln - 1]>\n";
1398 last if (!defined $rawlines[$ln - 1]);
1399 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1400 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1401 ($edge) = $1;
1402 last;
1405 if (defined $edge && $edge eq '*/') {
1406 $in_comment = 1;
1409 # Guestimate if this is a continuing comment. If this
1410 # is the start of a diff block and this line starts
1411 # ' *' then it is very likely a comment.
1412 if (!defined $edge &&
1413 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1415 $in_comment = 1;
1418 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1419 sanitise_line_reset($in_comment);
1421 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1422 # Standardise the strings and chars within the input to
1423 # simplify matching -- only bother with positive lines.
1424 $line = sanitise_line($rawline);
1426 push(@lines, $line);
1428 if ($realcnt > 1) {
1429 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1430 } else {
1431 $realcnt = 0;
1434 #print "==>$rawline\n";
1435 #print "-->$line\n";
1437 if ($setup_docs && $line =~ /^\+/) {
1438 push(@setup_docs, $line);
1442 $prefix = '';
1444 $realcnt = 0;
1445 $linenr = 0;
1446 foreach my $line (@lines) {
1447 $linenr++;
1449 my $rawline = $rawlines[$linenr - 1];
1451 #extract the line range in the file after the patch is applied
1452 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1453 $is_patch = 1;
1454 $first_line = $linenr + 1;
1455 $realline=$1-1;
1456 if (defined $2) {
1457 $realcnt=$3+1;
1458 } else {
1459 $realcnt=1+1;
1461 annotate_reset();
1462 $prev_values = 'E';
1464 %suppress_ifbraces = ();
1465 %suppress_whiletrailers = ();
1466 %suppress_export = ();
1467 next;
1469 # track the line number as we move through the hunk, note that
1470 # new versions of GNU diff omit the leading space on completely
1471 # blank context lines so we need to count that too.
1472 } elsif ($line =~ /^( |\+|$)/) {
1473 $realline++;
1474 $realcnt-- if ($realcnt != 0);
1476 # Measure the line length and indent.
1477 ($length, $indent) = line_stats($rawline);
1479 # Track the previous line.
1480 ($prevline, $stashline) = ($stashline, $line);
1481 ($previndent, $stashindent) = ($stashindent, $indent);
1482 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1484 #warn "line<$line>\n";
1486 } elsif ($realcnt == 1) {
1487 $realcnt--;
1490 my $hunk_line = ($realcnt != 0);
1492 #make up the handle for any error we report on this line
1493 $prefix = "$filename:$realline: " if ($emacs && $file);
1494 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1496 $here = "#$linenr: " if (!$file);
1497 $here = "#$realline: " if ($file);
1499 # extract the filename as it passes
1500 if ($line =~ /^diff --git.*?(\S+)$/) {
1501 $realfile = $1;
1502 $realfile =~ s@^([^/]*)/@@;
1504 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1505 $realfile = $1;
1506 $realfile =~ s@^([^/]*)/@@;
1508 $p1_prefix = $1;
1509 if (!$file && $tree && $p1_prefix ne '' &&
1510 -e "$root/$p1_prefix") {
1511 WARN("PATCH_PREFIX",
1512 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1515 if ($realfile =~ m@^include/asm/@) {
1516 ERROR("MODIFIED_INCLUDE_ASM",
1517 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1519 next;
1522 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1524 my $hereline = "$here\n$rawline\n";
1525 my $herecurr = "$here\n$rawline\n";
1526 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1528 $cnt_lines++ if ($realcnt != 0);
1530 # Check for incorrect file permissions
1531 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1532 my $permhere = $here . "FILE: $realfile\n";
1533 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1534 ERROR("EXECUTE_PERMISSIONS",
1535 "do not set execute permissions for source files\n" . $permhere);
1539 # Check the patch for a signoff:
1540 if ($line =~ /^\s*signed-off-by:/i) {
1541 $signoff++;
1544 # Check signature styles
1545 if ($line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1546 my $space_before = $1;
1547 my $sign_off = $2;
1548 my $space_after = $3;
1549 my $email = $4;
1550 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1552 if (defined $space_before && $space_before ne "") {
1553 WARN("BAD_SIGN_OFF",
1554 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1556 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1557 WARN("BAD_SIGN_OFF",
1558 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1560 if (!defined $space_after || $space_after ne " ") {
1561 WARN("BAD_SIGN_OFF",
1562 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
1565 my ($email_name, $email_address, $comment) = parse_email($email);
1566 my $suggested_email = format_email(($email_name, $email_address));
1567 if ($suggested_email eq "") {
1568 ERROR("BAD_SIGN_OFF",
1569 "Unrecognized email address: '$email'\n" . $herecurr);
1570 } else {
1571 my $dequoted = $suggested_email;
1572 $dequoted =~ s/^"//;
1573 $dequoted =~ s/" </ </;
1574 # Don't force email to have quotes
1575 # Allow just an angle bracketed address
1576 if ("$dequoted$comment" ne $email &&
1577 "<$email_address>$comment" ne $email &&
1578 "$suggested_email$comment" ne $email) {
1579 WARN("BAD_SIGN_OFF",
1580 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1585 # Check for wrappage within a valid hunk of the file
1586 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1587 ERROR("CORRUPTED_PATCH",
1588 "patch seems to be corrupt (line wrapped?)\n" .
1589 $herecurr) if (!$emitted_corrupt++);
1592 # Check for absolute kernel paths.
1593 if ($tree) {
1594 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1595 my $file = $1;
1597 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1598 check_absolute_file($1, $herecurr)) {
1600 } else {
1601 check_absolute_file($file, $herecurr);
1606 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1607 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1608 $rawline !~ m/^$UTF8*$/) {
1609 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1611 my $blank = copy_spacing($rawline);
1612 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1613 my $hereptr = "$hereline$ptr\n";
1615 CHK("INVALID_UTF8",
1616 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1619 # ignore non-hunk lines and lines being removed
1620 next if (!$hunk_line || $line =~ /^-/);
1622 #trailing whitespace
1623 if ($line =~ /^\+.*\015/) {
1624 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1625 ERROR("DOS_LINE_ENDINGS",
1626 "DOS line endings\n" . $herevet);
1628 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1629 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1630 ERROR("TRAILING_WHITESPACE",
1631 "trailing whitespace\n" . $herevet);
1632 $rpt_cleaners = 1;
1635 # check for Kconfig help text having a real description
1636 # Only applies when adding the entry originally, after that we do not have
1637 # sufficient context to determine whether it is indeed long enough.
1638 if ($realfile =~ /Kconfig/ &&
1639 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1640 my $length = 0;
1641 my $cnt = $realcnt;
1642 my $ln = $linenr + 1;
1643 my $f;
1644 my $is_end = 0;
1645 while ($cnt > 0 && defined $lines[$ln - 1]) {
1646 $f = $lines[$ln - 1];
1647 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1648 $is_end = $lines[$ln - 1] =~ /^\+/;
1649 $ln++;
1651 next if ($f =~ /^-/);
1652 $f =~ s/^.//;
1653 $f =~ s/#.*//;
1654 $f =~ s/^\s+//;
1655 next if ($f =~ /^$/);
1656 if ($f =~ /^\s*config\s/) {
1657 $is_end = 1;
1658 last;
1660 $length++;
1662 WARN("CONFIG_DESCRIPTION",
1663 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1664 #print "is_end<$is_end> length<$length>\n";
1667 # check we are in a valid source file if not then ignore this hunk
1668 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1670 #120 column limit
1671 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1672 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1673 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1674 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1675 $length > 120)
1677 WARN("LONG_LINE",
1678 "line over 120 characters\n" . $herecurr);
1681 # check for spaces before a quoted newline
1682 if ($rawline =~ /^.*\".*\s\\n/) {
1683 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1684 "unnecessary whitespace before a quoted newline\n" . $herecurr);
1687 # check for adding lines without a newline.
1688 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1689 WARN("MISSING_EOF_NEWLINE",
1690 "adding a line without newline at end of file\n" . $herecurr);
1693 # Blackfin: use hi/lo macros
1694 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1695 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1696 my $herevet = "$here\n" . cat_vet($line) . "\n";
1697 ERROR("LO_MACRO",
1698 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1700 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1701 my $herevet = "$here\n" . cat_vet($line) . "\n";
1702 ERROR("HI_MACRO",
1703 "use the HI() macro, not (... >> 16)\n" . $herevet);
1707 # check we are in a valid source file C or perl if not then ignore this hunk
1708 next if ($realfile !~ /\.(h|c|pl)$/);
1710 # at the beginning of a line any tabs must come first and anything
1711 # more than 8 must use tabs.
1712 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1713 $rawline =~ /^\+\s* \s*/) {
1714 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1715 ERROR("CODE_INDENT",
1716 "code indent should use tabs where possible\n" . $herevet);
1717 $rpt_cleaners = 1;
1720 # check for space before tabs.
1721 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1722 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1723 WARN("SPACE_BEFORE_TAB",
1724 "please, no space before tabs\n" . $herevet);
1727 # check for spaces at the beginning of a line.
1728 # Exceptions:
1729 # 1) within comments
1730 # 2) indented preprocessor commands
1731 # 3) hanging labels
1732 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1733 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1734 WARN("LEADING_SPACE",
1735 "please, no spaces at the start of a line\n" . $herevet);
1738 # check we are in a valid C source file if not then ignore this hunk
1739 next if ($realfile !~ /\.(h|c)$/);
1741 # check for RCS/CVS revision markers
1742 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1743 WARN("CVS_KEYWORD",
1744 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1747 # Blackfin: don't use __builtin_bfin_[cs]sync
1748 if ($line =~ /__builtin_bfin_csync/) {
1749 my $herevet = "$here\n" . cat_vet($line) . "\n";
1750 ERROR("CSYNC",
1751 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1753 if ($line =~ /__builtin_bfin_ssync/) {
1754 my $herevet = "$here\n" . cat_vet($line) . "\n";
1755 ERROR("SSYNC",
1756 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1759 # Check for potential 'bare' types
1760 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1761 $realline_next);
1762 if ($realcnt && $line =~ /.\s*\S/) {
1763 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1764 ctx_statement_block($linenr, $realcnt, 0);
1765 $stat =~ s/\n./\n /g;
1766 $cond =~ s/\n./\n /g;
1768 # Find the real next line.
1769 $realline_next = $line_nr_next;
1770 if (defined $realline_next &&
1771 (!defined $lines[$realline_next - 1] ||
1772 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1773 $realline_next++;
1776 my $s = $stat;
1777 $s =~ s/{.*$//s;
1779 # Ignore goto labels.
1780 if ($s =~ /$Ident:\*$/s) {
1782 # Ignore functions being called
1783 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1785 } elsif ($s =~ /^.\s*else\b/s) {
1787 # declarations always start with types
1788 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1789 my $type = $1;
1790 $type =~ s/\s+/ /g;
1791 possible($type, "A:" . $s);
1793 # definitions in global scope can only start with types
1794 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1795 possible($1, "B:" . $s);
1798 # any (foo ... *) is a pointer cast, and foo is a type
1799 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1800 possible($1, "C:" . $s);
1803 # Check for any sort of function declaration.
1804 # int foo(something bar, other baz);
1805 # void (*store_gdt)(x86_descr_ptr *);
1806 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1807 my ($name_len) = length($1);
1809 my $ctx = $s;
1810 substr($ctx, 0, $name_len + 1, '');
1811 $ctx =~ s/\)[^\)]*$//;
1813 for my $arg (split(/\s*,\s*/, $ctx)) {
1814 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1816 possible($1, "D:" . $s);
1824 # Checks which may be anchored in the context.
1827 # Check for switch () and associated case and default
1828 # statements should be at the same indent.
1829 # if ($line=~/\bswitch\s*\(.*\)/) {
1830 # my $err = '';
1831 # my $sep = '';
1832 # my @ctx = ctx_block_outer($linenr, $realcnt);
1833 # shift(@ctx);
1834 # for my $ctx (@ctx) {
1835 # my ($clen, $cindent) = line_stats($ctx);
1836 # if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1837 # $indent != $cindent) {
1838 # $err .= "$sep$ctx\n";
1839 # $sep = '';
1840 # } else {
1841 # $sep = "[...]\n";
1844 # if ($err ne '') {
1845 # ERROR("SWITCH_CASE_INDENT_LEVEL",
1846 # "switch and case should be at the same indent\n$hereline$err");
1850 # if/while/etc brace do not go on next line, unless defining a do while loop,
1851 # or if that brace on the next line is for something else
1852 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1853 my $pre_ctx = "$1$2";
1855 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1856 my $ctx_cnt = $realcnt - $#ctx - 1;
1857 my $ctx = join("\n", @ctx);
1859 my $ctx_ln = $linenr;
1860 my $ctx_skip = $realcnt;
1862 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1863 defined $lines[$ctx_ln - 1] &&
1864 $lines[$ctx_ln - 1] =~ /^-/)) {
1865 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1866 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1867 $ctx_ln++;
1870 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1871 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1873 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1874 ERROR("OPEN_BRACE",
1875 "that open brace { should be on the previous line\n" .
1876 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1878 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1879 $ctx =~ /\)\s*\;\s*$/ &&
1880 defined $lines[$ctx_ln - 1])
1882 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1883 if ($nindent > $indent) {
1884 WARN("TRAILING_SEMICOLON",
1885 "trailing semicolon indicates no statements, indent implies otherwise\n" .
1886 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1891 # Check relative indent for conditionals and blocks.
1892 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1893 my ($s, $c) = ($stat, $cond);
1895 substr($s, 0, length($c), '');
1897 # Make sure we remove the line prefixes as we have
1898 # none on the first line, and are going to readd them
1899 # where necessary.
1900 $s =~ s/\n./\n/gs;
1902 # Find out how long the conditional actually is.
1903 my @newlines = ($c =~ /\n/gs);
1904 my $cond_lines = 1 + $#newlines;
1906 # We want to check the first line inside the block
1907 # starting at the end of the conditional, so remove:
1908 # 1) any blank line termination
1909 # 2) any opening brace { on end of the line
1910 # 3) any do (...) {
1911 my $continuation = 0;
1912 my $check = 0;
1913 $s =~ s/^.*\bdo\b//;
1914 $s =~ s/^\s*{//;
1915 if ($s =~ s/^\s*\\//) {
1916 $continuation = 1;
1918 if ($s =~ s/^\s*?\n//) {
1919 $check = 1;
1920 $cond_lines++;
1923 # Also ignore a loop construct at the end of a
1924 # preprocessor statement.
1925 if (($prevline =~ /^.\s*#\s*define\s/ ||
1926 $prevline =~ /\\\s*$/) && $continuation == 0) {
1927 $check = 0;
1930 my $cond_ptr = -1;
1931 $continuation = 0;
1932 while ($cond_ptr != $cond_lines) {
1933 $cond_ptr = $cond_lines;
1935 # If we see an #else/#elif then the code
1936 # is not linear.
1937 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1938 $check = 0;
1941 # Ignore:
1942 # 1) blank lines, they should be at 0,
1943 # 2) preprocessor lines, and
1944 # 3) labels.
1945 if ($continuation ||
1946 $s =~ /^\s*?\n/ ||
1947 $s =~ /^\s*#\s*?/ ||
1948 $s =~ /^\s*$Ident\s*:/) {
1949 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1950 if ($s =~ s/^.*?\n//) {
1951 $cond_lines++;
1956 my (undef, $sindent) = line_stats("+" . $s);
1957 my $stat_real = raw_line($linenr, $cond_lines);
1959 # Check if either of these lines are modified, else
1960 # this is not this patch's fault.
1961 if (!defined($stat_real) ||
1962 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1963 $check = 0;
1965 if (defined($stat_real) && $cond_lines > 1) {
1966 $stat_real = "[...]\n$stat_real";
1969 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
1971 if ($check && (($sindent % 4) != 0 ||
1972 ($sindent <= $indent && $s ne ''))) {
1973 WARN("SUSPECT_CODE_INDENT",
1974 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1978 # Track the 'values' across context and added lines.
1979 my $opline = $line; $opline =~ s/^./ /;
1980 my ($curr_values, $curr_vars) =
1981 annotate_values($opline . "\n", $prev_values);
1982 $curr_values = $prev_values . $curr_values;
1983 if ($dbg_values) {
1984 my $outline = $opline; $outline =~ s/\t/ /g;
1985 print "$linenr > .$outline\n";
1986 print "$linenr > $curr_values\n";
1987 print "$linenr > $curr_vars\n";
1989 $prev_values = substr($curr_values, -1);
1991 #ignore lines not being added
1992 if ($line=~/^[^\+]/) {next;}
1994 # TEST: allow direct testing of the type matcher.
1995 if ($dbg_type) {
1996 if ($line =~ /^.\s*$Declare\s*$/) {
1997 ERROR("TEST_TYPE",
1998 "TEST: is type\n" . $herecurr);
1999 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2000 ERROR("TEST_NOT_TYPE",
2001 "TEST: is not type ($1 is)\n". $herecurr);
2003 next;
2005 # TEST: allow direct testing of the attribute matcher.
2006 if ($dbg_attr) {
2007 if ($line =~ /^.\s*$Modifier\s*$/) {
2008 ERROR("TEST_ATTR",
2009 "TEST: is attr\n" . $herecurr);
2010 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2011 ERROR("TEST_NOT_ATTR",
2012 "TEST: is not attr ($1 is)\n". $herecurr);
2014 next;
2017 # check for initialisation to aggregates open brace on the next line
2018 if ($line =~ /^.\s*{/ &&
2019 $prevline =~ /(?:^|[^=])=\s*$/) {
2020 ERROR("OPEN_BRACE",
2021 "that open brace { should be on the previous line\n" . $hereprev);
2025 # Checks which are anchored on the added line.
2028 # check for malformed paths in #include statements (uses RAW line)
2029 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2030 my $path = $1;
2031 if ($path =~ m{//}) {
2032 ERROR("MALFORMED_INCLUDE",
2033 "malformed #include filename\n" .
2034 $herecurr);
2038 # no C99 // comments
2039 if ($line =~ m{//}) {
2040 ERROR("C99_COMMENTS",
2041 "do not use C99 // comments\n" . $herecurr);
2043 # Remove C99 comments.
2044 $line =~ s@//.*@@;
2045 $opline =~ s@//.*@@;
2047 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2048 # the whole statement.
2049 #print "APW <$lines[$realline_next - 1]>\n";
2050 if (defined $realline_next &&
2051 exists $lines[$realline_next - 1] &&
2052 !defined $suppress_export{$realline_next} &&
2053 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2054 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2055 # Handle definitions which produce identifiers with
2056 # a prefix:
2057 # XXX(foo);
2058 # EXPORT_SYMBOL(something_foo);
2059 my $name = $1;
2060 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
2061 $name =~ /^${Ident}_$2/) {
2062 #print "FOO C name<$name>\n";
2063 $suppress_export{$realline_next} = 1;
2065 } elsif ($stat !~ /(?:
2066 \n.}\s*$|
2067 ^.DEFINE_$Ident\(\Q$name\E\)|
2068 ^.DECLARE_$Ident\(\Q$name\E\)|
2069 ^.LIST_HEAD\(\Q$name\E\)|
2070 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2071 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2072 )/x) {
2073 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2074 $suppress_export{$realline_next} = 2;
2075 } else {
2076 $suppress_export{$realline_next} = 1;
2079 if (!defined $suppress_export{$linenr} &&
2080 $prevline =~ /^.\s*$/ &&
2081 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2082 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2083 #print "FOO B <$lines[$linenr - 1]>\n";
2084 $suppress_export{$linenr} = 2;
2086 if (defined $suppress_export{$linenr} &&
2087 $suppress_export{$linenr} == 2) {
2088 WARN("EXPORT_SYMBOL",
2089 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2092 # check for global initialisers.
2093 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2094 ERROR("GLOBAL_INITIALISERS",
2095 "do not initialise globals to 0 or NULL\n" .
2096 $herecurr);
2098 # check for static initialisers.
2099 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2100 ERROR("INITIALISED_STATIC",
2101 "do not initialise statics to 0 or NULL\n" .
2102 $herecurr);
2105 # check for static const char * arrays.
2106 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2107 WARN("STATIC_CONST_CHAR_ARRAY",
2108 "static const char * array should probably be static const char * const\n" .
2109 $herecurr);
2112 # check for static char foo[] = "bar" declarations.
2113 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2114 WARN("STATIC_CONST_CHAR_ARRAY",
2115 "static char array declaration should probably be static const char\n" .
2116 $herecurr);
2119 # check for declarations of struct pci_device_id
2120 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2121 WARN("DEFINE_PCI_DEVICE_TABLE",
2122 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2125 # check for new typedefs, only function parameters and sparse annotations
2126 # make sense.
2127 # if ($line =~ /\btypedef\s/ &&
2128 # $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2129 # $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2130 # $line !~ /\b$typeTypedefs\b/ &&
2131 # $line !~ /\b__bitwise(?:__|)\b/) {
2132 # WARN("NEW_TYPEDEFS",
2133 # "do not add new typedefs\n" . $herecurr);
2136 # * goes on variable not on type
2137 # (char*[ const])
2138 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
2139 my ($from, $to) = ($1, $1);
2141 # Should start with a space.
2142 $to =~ s/^(\S)/ $1/;
2143 # Should not end with a space.
2144 $to =~ s/\s+$//;
2145 # '*'s should not have spaces between.
2146 while ($to =~ s/\*\s+\*/\*\*/) {
2149 #print "from<$from> to<$to>\n";
2150 if ($from ne $to) {
2151 ERROR("POINTER_LOCATION",
2152 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2154 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
2155 my ($from, $to, $ident) = ($1, $1, $2);
2157 # Should start with a space.
2158 $to =~ s/^(\S)/ $1/;
2159 # Should not end with a space.
2160 $to =~ s/\s+$//;
2161 # '*'s should not have spaces between.
2162 while ($to =~ s/\*\s+\*/\*\*/) {
2164 # Modifiers should have spaces.
2165 $to =~ s/(\b$Modifier$)/$1 /;
2167 #print "from<$from> to<$to> ident<$ident>\n";
2168 if ($from ne $to && $ident !~ /^$Modifier$/) {
2169 ERROR("POINTER_LOCATION",
2170 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2174 # # no BUG() or BUG_ON()
2175 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2176 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2177 # print "$herecurr";
2178 # $clean = 0;
2181 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2182 WARN("LINUX_VERSION_CODE",
2183 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2186 # check for uses of printk_ratelimit
2187 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2188 WARN("PRINTK_RATELIMITED",
2189 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2192 # printk should use KERN_* levels. Note that follow on printk's on the
2193 # same line do not need a level, so we use the current block context
2194 # to try and find and validate the current printk. In summary the current
2195 # printk includes all preceding printk's which have no newline on the end.
2196 # we assume the first bad printk is the one to report.
2197 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2198 my $ok = 0;
2199 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2200 #print "CHECK<$lines[$ln - 1]\n";
2201 # we have a preceding printk if it ends
2202 # with "\n" ignore it, else it is to blame
2203 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2204 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2205 $ok = 1;
2207 last;
2210 if ($ok == 0) {
2211 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2212 "printk() should include KERN_ facility level\n" . $herecurr);
2216 # function brace can't be on same line, except for #defines of do while,
2217 # or if closed on same line
2218 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2219 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2220 ERROR("OPEN_BRACE",
2221 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2224 # open braces for enum, union and struct go on the same line.
2225 if ($line =~ /^.\s*{/ &&
2226 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2227 ERROR("OPEN_BRACE",
2228 "open brace '{' following $1 go on the same line\n" . $hereprev);
2231 # missing space after union, struct or enum definition
2232 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2233 WARN("SPACING",
2234 "missing space after $1 definition\n" . $herecurr);
2237 # check for spacing round square brackets; allowed:
2238 # 1. with a type on the left -- int [] a;
2239 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2240 # 3. inside a curly brace -- = { [0...10] = 5 }
2241 while ($line =~ /(.*?\s)\[/g) {
2242 my ($where, $prefix) = ($-[1], $1);
2243 if ($prefix !~ /$Type\s+$/ &&
2244 ($where != 0 || $prefix !~ /^.\s+$/) &&
2245 $prefix !~ /{\s+$/) {
2246 ERROR("BRACKET_SPACE",
2247 "space prohibited before open square bracket '['\n" . $herecurr);
2251 # check for spaces between functions and their parentheses.
2252 while ($line =~ /($Ident)\s+\(/g) {
2253 my $name = $1;
2254 my $ctx_before = substr($line, 0, $-[1]);
2255 my $ctx = "$ctx_before$name";
2257 # Ignore those directives where spaces _are_ permitted.
2258 if ($name =~ /^(?:
2259 if|for|while|switch|return|case|
2260 volatile|__volatile__|
2261 __attribute__|format|__extension__|
2262 asm|__asm__)$/x)
2265 # cpp #define statements have non-optional spaces, ie
2266 # if there is a space between the name and the open
2267 # parenthesis it is simply not a parameter group.
2268 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2270 # cpp #elif statement condition may start with a (
2271 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2273 # If this whole things ends with a type its most
2274 # likely a typedef for a function.
2275 } elsif ($ctx =~ /$Type$/) {
2277 } else {
2278 WARN("SPACING",
2279 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2282 # Check operator spacing.
2283 if (!($line=~/\#\s*include/)) {
2284 my $ops = qr{
2285 <<=|>>=|<=|>=|==|!=|
2286 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2287 =>|->|<<|>>|<|>|=|!|~|
2288 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2289 \?|:
2291 my @elements = split(/($ops|;)/, $opline);
2292 my $off = 0;
2294 my $blank = copy_spacing($opline);
2296 for (my $n = 0; $n < $#elements; $n += 2) {
2297 $off += length($elements[$n]);
2299 # Pick up the preceding and succeeding characters.
2300 my $ca = substr($opline, 0, $off);
2301 my $cc = '';
2302 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2303 $cc = substr($opline, $off + length($elements[$n + 1]));
2305 my $cb = "$ca$;$cc";
2307 my $a = '';
2308 $a = 'V' if ($elements[$n] ne '');
2309 $a = 'W' if ($elements[$n] =~ /\s$/);
2310 $a = 'C' if ($elements[$n] =~ /$;$/);
2311 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2312 $a = 'O' if ($elements[$n] eq '');
2313 $a = 'E' if ($ca =~ /^\s*$/);
2315 my $op = $elements[$n + 1];
2317 my $c = '';
2318 if (defined $elements[$n + 2]) {
2319 $c = 'V' if ($elements[$n + 2] ne '');
2320 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2321 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2322 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2323 $c = 'O' if ($elements[$n + 2] eq '');
2324 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2325 } else {
2326 $c = 'E';
2329 my $ctx = "${a}x${c}";
2331 my $at = "(ctx:$ctx)";
2333 my $ptr = substr($blank, 0, $off) . "^";
2334 my $hereptr = "$hereline$ptr\n";
2336 # Pull out the value of this operator.
2337 my $op_type = substr($curr_values, $off + 1, 1);
2339 # Get the full operator variant.
2340 my $opv = $op . substr($curr_vars, $off, 1);
2342 # Ignore operators passed as parameters.
2343 if ($op_type ne 'V' &&
2344 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2346 # # Ignore comments
2347 # } elsif ($op =~ /^$;+$/) {
2349 # ; should have either the end of line or a space or \ after it
2350 } elsif ($op eq ';') {
2351 if ($ctx !~ /.x[WEBC]/ &&
2352 $cc !~ /^\\/ && $cc !~ /^;/) {
2353 ERROR("SPACING",
2354 "space required after that '$op' $at\n" . $hereptr);
2357 # // is a comment
2358 } elsif ($op eq '//') {
2360 # No spaces for:
2361 # ->
2362 # : when part of a bitfield
2363 } elsif ($op eq '->' || $opv eq ':B') {
2364 if ($ctx =~ /Wx.|.xW/) {
2365 ERROR("SPACING",
2366 "spaces prohibited around that '$op' $at\n" . $hereptr);
2369 # , must have a space on the right.
2370 } elsif ($op eq ',') {
2371 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2372 ERROR("SPACING",
2373 "space required after that '$op' $at\n" . $hereptr);
2376 # '*' as part of a type definition -- reported already.
2377 } elsif ($opv eq '*_') {
2378 #warn "'*' is part of type\n";
2380 # unary operators should have a space before and
2381 # none after. May be left adjacent to another
2382 # unary operator, or a cast
2383 } elsif ($op eq '!' || $op eq '~' ||
2384 $opv eq '*U' || $opv eq '-U' ||
2385 $opv eq '&U' || $opv eq '&&U') {
2386 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2387 ERROR("SPACING",
2388 "space required before that '$op' $at\n" . $hereptr);
2390 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2391 # A unary '*' may be const
2393 } elsif ($ctx =~ /.xW/) {
2394 ERROR("SPACING",
2395 "space prohibited after that '$op' $at\n" . $hereptr);
2398 # unary ++ and unary -- are allowed no space on one side.
2399 } elsif ($op eq '++' or $op eq '--') {
2400 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2401 ERROR("SPACING",
2402 "space required one side of that '$op' $at\n" . $hereptr);
2404 if ($ctx =~ /Wx[BE]/ ||
2405 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2406 ERROR("SPACING",
2407 "space prohibited before that '$op' $at\n" . $hereptr);
2409 if ($ctx =~ /ExW/) {
2410 ERROR("SPACING",
2411 "space prohibited after that '$op' $at\n" . $hereptr);
2415 # << and >> may either have or not have spaces both sides
2416 } elsif ($op eq '<<' or $op eq '>>' or
2417 $op eq '&' or $op eq '^' or $op eq '|' or
2418 $op eq '+' or $op eq '-' or
2419 $op eq '*' or $op eq '/' or
2420 $op eq '%')
2422 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2423 ERROR("SPACING",
2424 "need consistent spacing around '$op' $at\n" .
2425 $hereptr);
2428 # A colon needs no spaces before when it is
2429 # terminating a case value or a label.
2430 } elsif ($opv eq ':C' || $opv eq ':L') {
2431 if ($ctx =~ /Wx./) {
2432 ERROR("SPACING",
2433 "space prohibited before that '$op' $at\n" . $hereptr);
2436 # All the others need spaces both sides.
2437 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2438 my $ok = 0;
2440 # Ignore email addresses <foo@bar>
2441 if (($op eq '<' &&
2442 $cc =~ /^\S+\@\S+>/) ||
2443 ($op eq '>' &&
2444 $ca =~ /<\S+\@\S+$/))
2446 $ok = 1;
2449 # Ignore ?:
2450 if (($opv eq ':O' && $ca =~ /\?$/) ||
2451 ($op eq '?' && $cc =~ /^:/)) {
2452 $ok = 1;
2455 if ($ok == 0) {
2456 ERROR("SPACING",
2457 "spaces required around that '$op' $at\n" . $hereptr);
2460 $off += length($elements[$n + 1]);
2464 # check for multiple assignments
2465 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2466 CHK("MULTIPLE_ASSIGNMENTS",
2467 "multiple assignments should be avoided\n" . $herecurr);
2470 ## # check for multiple declarations, allowing for a function declaration
2471 ## # continuation.
2472 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2473 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2475 ## # Remove any bracketed sections to ensure we do not
2476 ## # falsly report the parameters of functions.
2477 ## my $ln = $line;
2478 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2479 ## }
2480 ## if ($ln =~ /,/) {
2481 ## WARN("MULTIPLE_DECLARATION",
2482 ## "declaring multiple variables together should be avoided\n" . $herecurr);
2483 ## }
2484 ## }
2486 #need space before brace following if, while, etc
2487 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2488 $line =~ /do{/) {
2489 ERROR("SPACING",
2490 "space required before the open brace '{'\n" . $herecurr);
2493 # closing brace should have a space following it when it has anything
2494 # on the line
2495 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2496 ERROR("SPACING",
2497 "space required after that close brace '}'\n" . $herecurr);
2500 # check spacing on square brackets
2501 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2502 ERROR("SPACING",
2503 "space prohibited after that open square bracket '['\n" . $herecurr);
2505 if ($line =~ /\s\]/) {
2506 ERROR("SPACING",
2507 "space prohibited before that close square bracket ']'\n" . $herecurr);
2510 # check spacing on parentheses
2511 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2512 $line !~ /for\s*\(\s+;/) {
2513 ERROR("SPACING",
2514 "space prohibited after that open parenthesis '('\n" . $herecurr);
2516 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2517 $line !~ /for\s*\(.*;\s+\)/ &&
2518 $line !~ /:\s+\)/) {
2519 ERROR("SPACING",
2520 "space prohibited before that close parenthesis ')'\n" . $herecurr);
2523 #goto labels aren't indented, allow a single space however
2524 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2525 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2526 WARN("INDENTED_LABEL",
2527 "labels should not be indented\n" . $herecurr);
2530 # Return is not a function.
2531 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2532 my $spacing = $1;
2533 my $value = $2;
2535 # Flatten any parentheses
2536 $value =~ s/\(/ \(/g;
2537 $value =~ s/\)/\) /g;
2538 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2539 $value !~ /(?:$Ident|-?$Constant)\s*
2540 $Compare\s*
2541 (?:$Ident|-?$Constant)/x &&
2542 $value =~ s/\([^\(\)]*\)/1/) {
2544 #print "value<$value>\n";
2545 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2546 ERROR("RETURN_PARENTHESES",
2547 "return is not a function, parentheses are not required\n" . $herecurr);
2549 } elsif ($spacing !~ /\s+/) {
2550 ERROR("SPACING",
2551 "space required before the open parenthesis '('\n" . $herecurr);
2554 # Return of what appears to be an errno should normally be -'ve
2555 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2556 my $name = $1;
2557 if ($name ne 'EOF' && $name ne 'ERROR') {
2558 WARN("USE_NEGATIVE_ERRNO",
2559 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2563 # typecasts on min/max could be min_t/max_t
2564 if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2565 if (defined $2 || defined $8) {
2566 my $call = $1;
2567 my $cast1 = deparenthesize($2);
2568 my $arg1 = $3;
2569 my $cast2 = deparenthesize($8);
2570 my $arg2 = $9;
2571 my $cast;
2573 if ($cast1 ne "" && $cast2 ne "") {
2574 $cast = "$cast1 or $cast2";
2575 } elsif ($cast1 ne "") {
2576 $cast = $cast1;
2577 } else {
2578 $cast = $cast2;
2580 WARN("MINMAX",
2581 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
2585 # Need a space before open parenthesis after if, while etc
2586 if ($line=~/\b(if|while|for|switch)\(/) {
2587 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
2590 # Check for illegal assignment in if conditional -- and check for trailing
2591 # statements after the conditional.
2592 if ($line =~ /do\s*(?!{)/) {
2593 my ($stat_next) = ctx_statement_block($line_nr_next,
2594 $remain_next, $off_next);
2595 $stat_next =~ s/\n./\n /g;
2596 ##print "stat<$stat> stat_next<$stat_next>\n";
2598 if ($stat_next =~ /^\s*while\b/) {
2599 # If the statement carries leading newlines,
2600 # then count those as offsets.
2601 my ($whitespace) =
2602 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2603 my $offset =
2604 statement_rawlines($whitespace) - 1;
2606 $suppress_whiletrailers{$line_nr_next +
2607 $offset} = 1;
2610 if (!defined $suppress_whiletrailers{$linenr} &&
2611 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2612 my ($s, $c) = ($stat, $cond);
2614 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2615 ERROR("ASSIGN_IN_IF",
2616 "do not use assignment in if condition\n" . $herecurr);
2619 # Find out what is on the end of the line after the
2620 # conditional.
2621 substr($s, 0, length($c), '');
2622 $s =~ s/\n.*//g;
2623 $s =~ s/$;//g; # Remove any comments
2624 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2625 $c !~ /}\s*while\s*/)
2627 # Find out how long the conditional actually is.
2628 my @newlines = ($c =~ /\n/gs);
2629 my $cond_lines = 1 + $#newlines;
2630 my $stat_real = '';
2632 $stat_real = raw_line($linenr, $cond_lines)
2633 . "\n" if ($cond_lines);
2634 if (defined($stat_real) && $cond_lines > 1) {
2635 $stat_real = "[...]\n$stat_real";
2638 ERROR("TRAILING_STATEMENTS",
2639 "trailing statements should be on next line\n" . $herecurr . $stat_real);
2643 # Check for bitwise tests written as boolean
2644 if ($line =~ /
2646 (?:\[|\(|\&\&|\|\|)
2647 \s*0[xX][0-9]+\s*
2648 (?:\&\&|\|\|)
2650 (?:\&\&|\|\|)
2651 \s*0[xX][0-9]+\s*
2652 (?:\&\&|\|\||\)|\])
2653 )/x)
2655 WARN("HEXADECIMAL_BOOLEAN_TEST",
2656 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2659 # if and else should not have general statements after it
2660 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2661 my $s = $1;
2662 $s =~ s/$;//g; # Remove any comments
2663 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2664 ERROR("TRAILING_STATEMENTS",
2665 "trailing statements should be on next line\n" . $herecurr);
2668 # if should not continue a brace
2669 if ($line =~ /}\s*if\b/) {
2670 ERROR("TRAILING_STATEMENTS",
2671 "trailing statements should be on next line\n" .
2672 $herecurr);
2674 # case and default should not have general statements after them
2675 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2676 $line !~ /\G(?:
2677 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2678 \s*return\s+
2679 )/xg)
2681 ERROR("TRAILING_STATEMENTS",
2682 "trailing statements should be on next line\n" . $herecurr);
2685 # Check for }<nl>else {, these must be at the same
2686 # indent level to be relevant to each other.
2687 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2688 $previndent == $indent) {
2689 ERROR("ELSE_AFTER_BRACE",
2690 "else should follow close brace '}'\n" . $hereprev);
2693 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2694 $previndent == $indent) {
2695 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2697 # Find out what is on the end of the line after the
2698 # conditional.
2699 substr($s, 0, length($c), '');
2700 $s =~ s/\n.*//g;
2702 if ($s =~ /^\s*;/) {
2703 ERROR("WHILE_AFTER_BRACE",
2704 "while should follow close brace '}'\n" . $hereprev);
2708 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2709 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2710 # print "No studly caps, use _\n";
2711 # print "$herecurr";
2712 # $clean = 0;
2715 #no spaces allowed after \ in define
2716 if ($line=~/\#\s*define.*\\\s$/) {
2717 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2718 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
2721 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2722 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2723 my $file = "$1.h";
2724 my $checkfile = "include/linux/$file";
2725 if (-f "$root/$checkfile" &&
2726 $realfile ne $checkfile &&
2727 $1 !~ /$allowed_asm_includes/)
2729 if ($realfile =~ m{^arch/}) {
2730 CHK("ARCH_INCLUDE_LINUX",
2731 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2732 } else {
2733 WARN("INCLUDE_LINUX",
2734 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2739 # multi-statement macros should be enclosed in a do while loop, grab the
2740 # first statement and ensure its the whole macro if its not enclosed
2741 # in a known good container
2742 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2743 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2744 my $ln = $linenr;
2745 my $cnt = $realcnt;
2746 my ($off, $dstat, $dcond, $rest);
2747 my $ctx = '';
2749 my $args = defined($1);
2751 # Find the end of the macro and limit our statement
2752 # search to that.
2753 while ($cnt > 0 && defined $lines[$ln - 1] &&
2754 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2756 $ctx .= $rawlines[$ln - 1] . "\n";
2757 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2758 $ln++;
2760 $ctx .= $rawlines[$ln - 1];
2762 ($dstat, $dcond, $ln, $cnt, $off) =
2763 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2764 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2765 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2767 # Extract the remainder of the define (if any) and
2768 # rip off surrounding spaces, and trailing \'s.
2769 $rest = '';
2770 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2771 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2772 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2773 $rest .= substr($lines[$ln - 1], $off) . "\n";
2774 $cnt--;
2776 $ln++;
2777 $off = 0;
2779 $rest =~ s/\\\n.//g;
2780 $rest =~ s/^\s*//s;
2781 $rest =~ s/\s*$//s;
2783 # Clean up the original statement.
2784 if ($args) {
2785 substr($dstat, 0, length($dcond), '');
2786 } else {
2787 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2789 $dstat =~ s/$;//g;
2790 $dstat =~ s/\\\n.//g;
2791 $dstat =~ s/^\s*//s;
2792 $dstat =~ s/\s*$//s;
2794 # Flatten any parentheses and braces
2795 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2796 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2797 $dstat =~ s/\[[^\{\}]*\]/1/)
2801 my $exceptions = qr{
2802 $Declare|
2803 module_param_named|
2804 MODULE_PARAM_DESC|
2805 DECLARE_PER_CPU|
2806 DEFINE_PER_CPU|
2807 __typeof__\(|
2808 union|
2809 struct|
2810 \.$Ident\s*=\s*|
2811 ^\"|\"$
2813 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2814 if ($rest ne '' && $rest ne ',') {
2815 if ($rest !~ /while\s*\(/ &&
2816 $dstat !~ /$exceptions/)
2818 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2819 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2822 } elsif ($ctx !~ /;/) {
2823 if ($dstat ne '' &&
2824 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2825 $dstat !~ /$exceptions/ &&
2826 $dstat !~ /^\.$Ident\s*=/ &&
2827 $dstat =~ /$Operators/)
2829 ERROR("COMPLEX_MACRO",
2830 "Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2835 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2836 # all assignments may have only one of the following with an assignment:
2838 # ALIGN(...)
2839 # VMLINUX_SYMBOL(...)
2840 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2841 WARN("MISSING_VMLINUX_SYMBOL",
2842 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2845 # check for redundant bracing round if etc
2846 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2847 my ($level, $endln, @chunks) =
2848 ctx_statement_full($linenr, $realcnt, 1);
2849 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2850 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2851 if ($#chunks > 0 && $level == 0) {
2852 my $allowed = 0;
2853 my $seen = 0;
2854 my $herectx = $here . "\n";
2855 my $ln = $linenr - 1;
2856 for my $chunk (@chunks) {
2857 my ($cond, $block) = @{$chunk};
2859 # If the condition carries leading newlines, then count those as offsets.
2860 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2861 my $offset = statement_rawlines($whitespace) - 1;
2863 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2865 # We have looked at and allowed this specific line.
2866 $suppress_ifbraces{$ln + $offset} = 1;
2868 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2869 $ln += statement_rawlines($block) - 1;
2871 substr($block, 0, length($cond), '');
2873 $seen++ if ($block =~ /^\s*{/);
2875 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2876 if (statement_lines($cond) > 1) {
2877 #print "APW: ALLOWED: cond<$cond>\n";
2878 $allowed = 1;
2880 if ($block =~/\b(?:if|for|while)\b/) {
2881 #print "APW: ALLOWED: block<$block>\n";
2882 $allowed = 1;
2884 if (statement_block_size($block) > 1) {
2885 #print "APW: ALLOWED: lines block<$block>\n";
2886 $allowed = 1;
2889 if ($seen && !$allowed) {
2890 WARN("BRACES",
2891 "braces {} are not necessary for any arm of this statement\n" . $herectx);
2895 if (!defined $suppress_ifbraces{$linenr - 1} &&
2896 $line =~ /\b(if|while|for|else)\b/) {
2897 my $allowed = 0;
2899 # Check the pre-context.
2900 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2901 #print "APW: ALLOWED: pre<$1>\n";
2902 $allowed = 1;
2905 my ($level, $endln, @chunks) =
2906 ctx_statement_full($linenr, $realcnt, $-[0]);
2908 # Check the condition.
2909 my ($cond, $block) = @{$chunks[0]};
2910 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2911 if (defined $cond) {
2912 substr($block, 0, length($cond), '');
2914 if (statement_lines($cond) > 1) {
2915 #print "APW: ALLOWED: cond<$cond>\n";
2916 $allowed = 1;
2918 if ($block =~/\b(?:if|for|while)\b/) {
2919 #print "APW: ALLOWED: block<$block>\n";
2920 $allowed = 1;
2922 if (statement_block_size($block) > 1) {
2923 #print "APW: ALLOWED: lines block<$block>\n";
2924 $allowed = 1;
2926 # Check the post-context.
2927 if (defined $chunks[1]) {
2928 my ($cond, $block) = @{$chunks[1]};
2929 if (defined $cond) {
2930 substr($block, 0, length($cond), '');
2932 if ($block =~ /^\s*\{/) {
2933 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2934 $allowed = 1;
2937 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2938 my $herectx = $here . "\n";
2939 my $cnt = statement_rawlines($block);
2941 for (my $n = 0; $n < $cnt; $n++) {
2942 $herectx .= raw_line($linenr, $n) . "\n";
2945 WARN("BRACES",
2946 "braces {} are not necessary for single statement blocks\n" . $herectx);
2950 # don't include deprecated include files (uses RAW line)
2951 for my $inc (@dep_includes) {
2952 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2953 ERROR("DEPRECATED_INCLUDE",
2954 "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2958 # don't use deprecated functions
2959 for my $func (@dep_functions) {
2960 if ($line =~ /\b$func\b/) {
2961 ERROR("DEPRECATED_FUNCTION",
2962 "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2966 # no volatiles please
2967 # my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2968 # if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2969 # WARN("VOLATILE",
2970 # "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2973 # warn about #if 0
2974 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2975 CHK("REDUNDANT_CODE",
2976 "if this code is redundant consider removing it\n" .
2977 $herecurr);
2980 # check for needless kfree() checks
2981 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2982 my $expr = $1;
2983 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2984 WARN("NEEDLESS_KFREE",
2985 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2988 # check for needless usb_free_urb() checks
2989 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2990 my $expr = $1;
2991 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2992 WARN("NEEDLESS_USB_FREE_URB",
2993 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2997 # prefer usleep_range over udelay
2998 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2999 # ignore udelay's < 10, however
3000 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3001 CHK("USLEEP_RANGE",
3002 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3006 # warn about unexpectedly long msleep's
3007 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3008 if ($1 < 20) {
3009 WARN("MSLEEP",
3010 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3014 # warn about #ifdefs in C files
3015 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3016 # print "#ifdef in C files should be avoided\n";
3017 # print "$herecurr";
3018 # $clean = 0;
3021 # warn about spacing in #ifdefs
3022 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3023 ERROR("SPACING",
3024 "exactly one space required after that #$1\n" . $herecurr);
3027 # check for spinlock_t definitions without a comment.
3028 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3029 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3030 my $which = $1;
3031 if (!ctx_has_comment($first_line, $linenr)) {
3032 CHK("UNCOMMENTED_DEFINITION",
3033 "$1 definition without comment\n" . $herecurr);
3036 # check for memory barriers without a comment.
3037 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3038 if (!ctx_has_comment($first_line, $linenr)) {
3039 CHK("MEMORY_BARRIER",
3040 "memory barrier without comment\n" . $herecurr);
3043 # check of hardware specific defines
3044 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3045 CHK("ARCH_DEFINES",
3046 "architecture specific defines should be avoided\n" . $herecurr);
3049 # Check that the storage class is at the beginning of a declaration
3050 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3051 WARN("STORAGE_CLASS",
3052 "storage class should be at the beginning of the declaration\n" . $herecurr)
3055 # check the location of the inline attribute, that it is between
3056 # storage class and type.
3057 if ($line =~ /\b$Type\s+$Inline\b/ ||
3058 $line =~ /\b$Inline\s+$Storage\b/) {
3059 ERROR("INLINE_LOCATION",
3060 "inline keyword should sit between storage class and type\n" . $herecurr);
3063 # Check for __inline__ and __inline, prefer inline
3064 if ($line =~ /\b(__inline__|__inline)\b/) {
3065 WARN("INLINE",
3066 "plain inline is preferred over $1\n" . $herecurr);
3069 # Check for __attribute__ packed, prefer __packed
3070 # if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3071 # WARN("PREFER_PACKED",
3072 # "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3075 # Check for __attribute__ aligned, prefer __aligned
3076 # if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3077 # WARN("PREFER_ALIGNED",
3078 # "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3081 # check for sizeof(&)
3082 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3083 WARN("SIZEOF_ADDRESS",
3084 "sizeof(& should be avoided\n" . $herecurr);
3087 # check for line continuations in quoted strings with odd counts of "
3088 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3089 WARN("LINE_CONTINUATIONS",
3090 "Avoid line continuations in quoted strings\n" . $herecurr);
3093 # check for new externs in .c files.
3094 # if ($realfile =~ /\.c$/ && defined $stat &&
3095 # $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3097 # my $function_name = $1;
3098 # my $paren_space = $2;
3100 # my $s = $stat;
3101 # if (defined $cond) {
3102 # substr($s, 0, length($cond), '');
3104 # if ($s =~ /^\s*;/ &&
3105 # $function_name ne 'uninitialized_var')
3107 # WARN("AVOID_EXTERNS",
3108 # "externs should be avoided in .c files\n" . $herecurr);
3111 # if ($paren_space =~ /\n/) {
3112 # WARN("FUNCTION_ARGUMENTS",
3113 # "arguments for function declarations should follow identifier\n" . $herecurr);
3116 # } elsif ($realfile =~ /\.c$/ && defined $stat &&
3117 # $stat =~ /^.\s*extern\s+/)
3119 # WARN("AVOID_EXTERNS",
3120 # "externs should be avoided in .c files\n" . $herecurr);
3123 # checks for new __setup's
3124 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3125 my $name = $1;
3127 if (!grep(/$name/, @setup_docs)) {
3128 CHK("UNDOCUMENTED_SETUP",
3129 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3133 # check for pointless casting of kmalloc return
3134 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3135 WARN("UNNECESSARY_CASTS",
3136 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3139 # check for multiple semicolons
3140 if ($line =~ /;\s*;\s*$/) {
3141 WARN("ONE_SEMICOLON",
3142 "Statements terminations use 1 semicolon\n" . $herecurr);
3145 # check for gcc specific __FUNCTION__
3146 if ($line =~ /__FUNCTION__/) {
3147 WARN("USE_FUNC",
3148 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3151 # check for semaphores initialized locked
3152 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3153 WARN("CONSIDER_COMPLETION",
3154 "consider using a completion\n" . $herecurr);
3157 # recommend kstrto* over simple_strto*
3158 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
3159 WARN("CONSIDER_KSTRTO",
3160 "consider using kstrto* in preference to simple_$1\n" . $herecurr);
3162 # check for __initcall(), use device_initcall() explicitly please
3163 if ($line =~ /^.\s*__initcall\s*\(/) {
3164 WARN("USE_DEVICE_INITCALL",
3165 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3167 # check for various ops structs, ensure they are const.
3168 my $struct_ops = qr{acpi_dock_ops|
3169 address_space_operations|
3170 backlight_ops|
3171 block_device_operations|
3172 dentry_operations|
3173 dev_pm_ops|
3174 dma_map_ops|
3175 extent_io_ops|
3176 file_lock_operations|
3177 file_operations|
3178 hv_ops|
3179 ide_dma_ops|
3180 intel_dvo_dev_ops|
3181 item_operations|
3182 iwl_ops|
3183 kgdb_arch|
3184 kgdb_io|
3185 kset_uevent_ops|
3186 lock_manager_operations|
3187 microcode_ops|
3188 mtrr_ops|
3189 neigh_ops|
3190 nlmsvc_binding|
3191 pci_raw_ops|
3192 pipe_buf_operations|
3193 platform_hibernation_ops|
3194 platform_suspend_ops|
3195 proto_ops|
3196 rpc_pipe_ops|
3197 seq_operations|
3198 snd_ac97_build_ops|
3199 soc_pcmcia_socket_ops|
3200 stacktrace_ops|
3201 sysfs_ops|
3202 tty_operations|
3203 usb_mon_operations|
3204 wd_ops}x;
3205 if ($line !~ /\bconst\b/ &&
3206 $line =~ /\bstruct\s+($struct_ops)\b/) {
3207 WARN("CONST_STRUCT",
3208 "struct $1 should normally be const\n" .
3209 $herecurr);
3212 # use of NR_CPUS is usually wrong
3213 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3214 if ($line =~ /\bNR_CPUS\b/ &&
3215 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3216 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3217 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3218 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3219 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3221 WARN("NR_CPUS",
3222 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3225 # check for %L{u,d,i} in strings
3226 my $string;
3227 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3228 $string = substr($rawline, $-[1], $+[1] - $-[1]);
3229 $string =~ s/%%/__/g;
3230 if ($string =~ /(?<!%)%L[udi]/) {
3231 WARN("PRINTF_L",
3232 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3233 last;
3237 # whine mightly about in_atomic
3238 if ($line =~ /\bin_atomic\s*\(/) {
3239 if ($realfile =~ m@^drivers/@) {
3240 ERROR("IN_ATOMIC",
3241 "do not use in_atomic in drivers\n" . $herecurr);
3242 } elsif ($realfile !~ m@^kernel/@) {
3243 WARN("IN_ATOMIC",
3244 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3248 # check for lockdep_set_novalidate_class
3249 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3250 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3251 if ($realfile !~ m@^kernel/lockdep@ &&
3252 $realfile !~ m@^include/linux/lockdep@ &&
3253 $realfile !~ m@^drivers/base/core@) {
3254 ERROR("LOCKDEP",
3255 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3259 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3260 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3261 WARN("EXPORTED_WORLD_WRITABLE",
3262 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3265 # Check for memset with swapped arguments
3266 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3267 ERROR("MEMSET",
3268 "memset size is 3rd argument, not the second.\n" . $herecurr);
3272 # If we have no input at all, then there is nothing to report on
3273 # so just keep quiet.
3274 if ($#rawlines == -1) {
3275 exit(0);
3278 # In mailback mode only produce a report in the negative, for
3279 # things that appear to be patches.
3280 if ($mailback && ($clean == 1 || !$is_patch)) {
3281 exit(0);
3284 # This is not a patch, and we are are in 'no-patch' mode so
3285 # just keep quiet.
3286 if (!$chk_patch && !$is_patch) {
3287 exit(0);
3290 if (!$is_patch) {
3291 ERROR("NOT_UNIFIED_DIFF",
3292 "Does not appear to be a unified-diff format patch\n");
3294 if ($is_patch && $chk_signoff && $signoff == 0) {
3295 ERROR("MISSING_SIGN_OFF",
3296 "Missing Signed-off-by: line(s)\n");
3299 print report_dump();
3300 if ($summary && !($clean == 1 && $quiet == 1)) {
3301 print "$filename " if ($summary_file);
3302 print "total: $cnt_error errors, $cnt_warn warnings, " .
3303 (($check)? "$cnt_chk checks, " : "") .
3304 "$cnt_lines lines checked\n";
3305 print "\n" if ($quiet == 0);
3308 if ($quiet == 0) {
3309 # If there were whitespace errors which cleanpatch can fix
3310 # then suggest that.
3311 if ($rpt_cleaners) {
3312 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3313 print " scripts/cleanfile\n\n";
3314 $rpt_cleaners = 0;
3318 if (keys %ignore_type) {
3319 print "NOTE: Ignored message types:";
3320 foreach my $ignore (sort keys %ignore_type) {
3321 print " $ignore";
3323 print "\n";
3324 print "\n" if ($quiet == 0);
3327 if ($clean == 1 && $quiet == 0) {
3328 print "$vname has no obvious style problems and is ready for submission.\n"
3330 if ($clean == 0 && $quiet == 0) {
3331 print << "EOM";
3332 $vname has style problems, please review.
3334 If any of these errors are false positives, please report
3335 them to the openocd-devel mailing list or prepare a patch
3336 and send it to Gerrit for review.
3340 return $clean;