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 # Based on linux/scripts/checkpatch.pl from 2.6.38 but uses Lustre
9 # specific deprecated symbol/functions/include lists rather than
10 # Documentation/feature-removal-schedule.txt.
19 use Getopt::Long qw(:config no_auto_abbrev);
39 my $configuration_file = ".checkpatch.conf";
45 Usage: $P [OPTION]... [FILE]...
50 --no-tree run without a kernel tree
51 --no-signoff do not check for 'Signed-off-by' line
52 --patch treat FILE as patchfile (default)
53 --emacs emacs compile window format
54 --terse one line per report
55 -f, --file treat FILE as regular source file
56 --subjective, --strict enable more subjective tests
57 --ignore TYPE(,TYPE2...) ignore various comma separated message types
58 --show-types show the message "types" in the output
59 --root=PATH PATH to the kernel tree root
60 --no-summary suppress the per-file summary
61 --mailback only produce a report in case of warnings/errors
62 --summary-file include the filename in summary
63 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
64 'values', 'possible', 'type', and 'attr' (default
66 --test-only=WORD report only warnings/errors containing WORD
68 -h, --help, --version display this help and exit
70 When FILE is - read standard input.
76 my $conf = which_conf($configuration_file);
79 open(my $conffile, '<', "$conf")
80 or warn "$P: Can't find a readable $configuration_file file $!\n";
85 $line =~ s/\s*\n?$//g;
89 next if ($line =~ m/^\s*#/);
90 next if ($line =~ m/^\s*$/);
92 my @words = split(" ", $line);
93 foreach my $word (@words) {
94 last if ($word =~ m/^#/);
95 push (@conf_args, $word);
99 unshift(@ARGV, @conf_args) if @conf_args;
103 'q|quiet+' => \$quiet,
105 'signoff!' => \$chk_signoff,
106 'patch!' => \$chk_patch,
110 'subjective!' => \$check,
111 'strict!' => \$check,
112 'ignore=s' => \@ignore,
113 'show-types!' => \$show_types,
115 'summary!' => \$summary,
116 'mailback!' => \$mailback,
117 'summary-file!' => \$summary_file,
119 'debug=s' => \%debug,
120 'test-only=s' => \$tst_only,
130 print "$P: no input files\n";
134 @ignore = split(/,/, join(',',@ignore));
135 foreach my $word (@ignore) {
136 $word =~ s/\s*\n?$//g;
139 $word =~ tr/[a-z]/[A-Z]/;
141 next if ($word =~ m/^\s*#/);
142 next if ($word =~ m/^\s*$/);
144 $ignore_type{$word}++;
148 my $dbg_possible = 0;
151 for my $key (keys %debug) {
153 eval "\${dbg_$key} = '$debug{$key}';";
157 my $rpt_cleaners = 0;
166 if (!top_of_kernel_tree($root)) {
167 die "$P: $root: --root does not point at a valid tree\n";
170 if (top_of_kernel_tree('.')) {
172 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
173 top_of_kernel_tree($1)) {
178 if (!defined $root) {
179 print "Must be run from the top-level dir. of a kernel tree\n";
184 my $emitted_corrupt = 0;
187 [A-Za-z_][A-Za-z\d_]*
188 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
190 our $Storage = qr{extern|static|asmlinkage};
203 # Notes to $Attribute:
204 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
223 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
224 ____cacheline_aligned|
225 ____cacheline_aligned_in_smp|
226 ____cacheline_internodealigned_in_smp|
230 our $Inline = qr{inline|__always_inline|noinline};
231 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
232 our $Lval = qr{$Ident(?:$Member)*};
234 our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
235 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
236 our $Compare = qr{<=|>=|==|!=|<|>};
240 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
247 our $NON_ASCII_UTF8 = qr{
248 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
249 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
250 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
251 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
252 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
253 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
254 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
258 [\x09\x0A\x0D\x20-\x7E] # ASCII
262 our $typeTypedefs = qr{(?x:
263 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
267 our $logFunctions = qr{(?x:
268 printk(?:_ratelimited|_once|)|
269 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
270 WARN(?:_RATELIMIT|_ONCE|)|
275 our $signature_tags = qr{(?xi:
287 qr{(?:unsigned\s+)?char},
288 qr{(?:unsigned\s+)?short},
289 qr{(?:unsigned\s+)?int},
290 qr{(?:unsigned\s+)?long},
291 qr{(?:unsigned\s+)?long\s+int},
292 qr{(?:unsigned\s+)?long\s+long},
293 qr{(?:unsigned\s+)?long\s+long\s+int},
302 qr{${Ident}_handler},
303 qr{${Ident}_handler_fn},
305 our @modifierList = (
309 our $allowed_asm_includes = qr{(?x:
313 # memory.h: ARM has a custom one
316 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
317 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
318 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
320 (?:$Modifier\s+|const\s+)*
322 (?:typeof|__typeof__)\s*\([^\)]*\)|
326 (?:\s+$Modifier|\s+const)*
330 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
331 (?:\s+$Inline|\s+$Modifier)*
333 $Declare = qr{(?:$Storage\s+)?$Type};
337 our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
339 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
340 our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
341 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
345 return "" if (!defined($string));
346 $string =~ s@^\s*\(\s*@@g;
347 $string =~ s@\s*\)\s*$@@g;
348 $string =~ s@\s+@ @g;
352 $chk_signoff = 0 if ($file);
355 'asm/types.h', 'linux/types.h',
356 'asm/uaccess.h', 'linux/uaccess.h',
359 my %dep_functions = (
360 'CFS_ATOMIC_INIT', 'ATOMIC_INIT',
361 'cfs_atomic_add', 'atomic_add',
362 'cfs_atomic_add_return', 'atomic_add_return',
363 'cfs_atomic_add_unless', 'atomic_add_unless',
364 'cfs_atomic_cmpxchg', 'atomic_cmpxchg',
365 'cfs_atomic_dec', 'atomic_dec',
366 'cfs_atomic_dec_and_lock', 'atomic_dec_and_lock',
367 'cfs_atomic_dec_and_test', 'atomic_dec_and_test',
368 'cfs_atomic_dec_return', 'atomic_dec_return',
369 'cfs_atomic_inc', 'atomic_inc',
370 'cfs_atomic_inc_and_test', 'atomic_inc_and_test',
371 'cfs_atomic_inc_not_zero', 'atomic_inc_not_zero',
372 'cfs_atomic_inc_return', 'atomic_inc_return',
373 'cfs_atomic_read', 'atomic_read',
374 'cfs_atomic_set', 'atomic_set',
375 'cfs_atomic_sub', 'atomic_sub',
376 'cfs_atomic_sub_and_test', 'atomic_sub_and_test',
377 'cfs_atomic_sub_return', 'atomic_sub_return',
378 'cfs_atomic_t', 'atomic_t',
380 'CFS_HLIST_HEAD', 'HLIST_HEAD',
381 'CFS_HLIST_HEAD_INIT', 'HLIST_HEAD_INIT',
382 'CFS_INIT_HLIST_HEAD', 'INIT_HLIST_HEAD',
383 'CFS_INIT_HLIST_NODE', 'INIT_HLIST_NODE',
384 'cfs_hlist_add_after', 'hlist_add_after',
385 'cfs_hlist_add_before', 'hlist_add_before',
386 'cfs_hlist_add_head', 'hlist_add_head',
387 'cfs_hlist_del', 'hlist_del',
388 'cfs_hlist_del_init', 'hlist_del_init',
389 'cfs_hlist_empty', 'hlist_empty',
390 'cfs_hlist_entry', 'hlist_entry',
391 'cfs_hlist_for_each', 'hlist_for_each',
392 'cfs_hlist_for_each_entry', 'hlist_for_each_entry',
393 'cfs_hlist_for_each_entry_continue',
394 'hlist_for_each_entry_continue',
395 'cfs_hlist_for_each_entry_from',
396 'hlist_for_each_entry_from',
397 'cfs_hlist_for_each_entry_safe',
398 'hlist_for_each_entry_safe',
399 'cfs_hlist_for_each_safe', 'hlist_for_each_safe',
400 'cfs_hlist_head_t', 'struct hlist_head',
401 'cfs_hlist_node_t', 'struct hlist_node',
402 'cfs_hlist_unhashed', 'hlist_unhashed',
404 'cfs_inode_t', 'struct inode',
406 'CFS_INIT_LIST_HEAD', 'INIT_LIST_HEAD',
407 'CFS_LIST_HEAD', 'struct list_head foo = LIST_HEAD_INIT(foo);',
408 'CFS_LIST_HEAD_INIT', 'LIST_HEAD_INIT',
409 'cfs_list_add', 'list_add',
410 'cfs_list_add_tail', 'list_add_tail',
411 'cfs_list_del', 'list_del',
412 'cfs_list_del_init', 'list_del_init',
413 'cfs_list_empty', 'list_empty',
414 'cfs_list_empty_careful', 'list_empty_careful',
415 'cfs_list_entry', 'list_entry',
416 'cfs_list_for_each', 'list_for_each',
417 'cfs_list_for_each_entry', 'list_for_each_entry',
418 'cfs_list_for_each_entry_continue',
419 'list_for_each_entry_continue',
420 'cfs_list_for_each_entry_reverse',
421 'list_for_each_entry_reverse',
422 'cfs_list_for_each_entry_safe',
423 'list_for_each_entry_safe',
424 'cfs_list_for_each_entry_safe_from',
425 'list_for_each_entry_safe_from',
426 'cfs_list_for_each_entry_safe_reverse',
427 'list_for_each_entry_safe_reverse',
428 'cfs_list_for_each_entry_safe_typed',
429 'list_for_each_entry_safe_typed',
430 'cfs_list_for_each_entry_typed',
431 'list_for_each_entry_typed',
432 'cfs_list_for_each_prev', 'list_for_each_prev',
433 'cfs_list_for_each_safe', 'list_for_each_safe',
434 'cfs_list_move', 'list_move',
435 'cfs_list_move_tail', 'list_move_tail',
436 'cfs_list_splice', 'list_splice',
437 'cfs_list_splice_init', 'list_splice_init',
438 'cfs_list_splice_tail', 'list_splice_tail',
439 'cfs_list_t', 'struct list_head',
441 'CFS_PAGE_MASK', 'PAGE_CACHE_MASK or PAGE_MASK',
442 'CFS_PAGE_SIZE', 'PAGE_CACHE_SIZE or PAGE_SIZE',
444 'cfs_proc_dir_entry_t', 'struct proc_dir_entry',
446 'cfs_rcu_head_t', 'struct rcu_head',
450 'sprintf', 'snprintf',
453 'tempnam', 'mkstemp',
459 for my $filename (@ARGV) {
462 open($FILE, '-|', "diff -u /dev/null $filename") ||
463 die "$P: $filename: diff failed - $!\n";
464 } elsif ($filename eq '-') {
465 open($FILE, '<&STDIN');
467 open($FILE, '<', "$filename") ||
468 die "$P: $filename: open failed - $!\n";
470 if ($filename eq '-') {
471 $vname = 'Your patch';
480 if (!process($filename)) {
489 sub top_of_kernel_tree {
493 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
494 "README", "Documentation", "arch", "include", "drivers",
495 "fs", "init", "ipc", "kernel", "lib", "scripts",
498 foreach my $check (@tree_check) {
499 if (! -e $root . '/' . $check) {
507 my ($formatted_email) = @_;
513 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
516 $comment = $3 if defined $3;
517 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
519 $comment = $2 if defined $2;
520 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
522 $comment = $2 if defined $2;
523 $formatted_email =~ s/$address.*$//;
524 $name = $formatted_email;
525 $name =~ s/^\s+|\s+$//g;
526 $name =~ s/^\"|\"$//g;
527 # If there's a name left after stripping spaces and
528 # leading quotes, and the address doesn't have both
529 # leading and trailing angle brackets, the address
531 # "joe smith joe@smith.com" bad
532 # "joe smith <joe@smith.com" bad
533 if ($name ne "" && $address !~ /^<[^>]+>$/) {
540 $name =~ s/^\s+|\s+$//g;
541 $name =~ s/^\"|\"$//g;
542 $address =~ s/^\s+|\s+$//g;
543 $address =~ s/^\<|\>$//g;
545 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
546 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
550 return ($name, $address, $comment);
554 my ($name, $address) = @_;
558 $name =~ s/^\s+|\s+$//g;
559 $name =~ s/^\"|\"$//g;
560 $address =~ s/^\s+|\s+$//g;
562 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
563 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
568 $formatted_email = "$address";
570 $formatted_email = "$name <$address>";
573 return $formatted_email;
579 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
580 if (-e "$path/$conf") {
581 return "$path/$conf";
593 for my $c (split(//, $str)) {
597 for (; ($n % 8) != 0; $n++) {
609 (my $res = shift) =~ tr/\t/ /c;
616 # Drop the diff line leader and expand tabs
618 $line = expand_tabs($line);
620 # Pick the indent from the front of the line.
621 my ($white) = ($line =~ /^(\s*)/);
623 return (length($line), length($white));
626 my $sanitise_quote = '';
628 sub sanitise_line_reset {
629 my ($in_comment) = @_;
632 $sanitise_quote = '*/';
634 $sanitise_quote = '';
647 # Always copy over the diff marker.
648 $res = substr($line, 0, 1);
650 for ($off = 1; $off < length($line); $off++) {
651 $c = substr($line, $off, 1);
653 # Comments we are wacking completly including the begin
654 # and end, all to $;.
655 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
656 $sanitise_quote = '*/';
658 substr($res, $off, 2, "$;$;");
662 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
663 $sanitise_quote = '';
664 substr($res, $off, 2, "$;$;");
668 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
669 $sanitise_quote = '//';
671 substr($res, $off, 2, $sanitise_quote);
676 # A \ in a string means ignore the next character.
677 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
679 substr($res, $off, 2, 'XX');
684 if ($c eq "'" || $c eq '"') {
685 if ($sanitise_quote eq '') {
686 $sanitise_quote = $c;
688 substr($res, $off, 1, $c);
690 } elsif ($sanitise_quote eq $c) {
691 $sanitise_quote = '';
695 #print "c<$c> SQ<$sanitise_quote>\n";
696 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
697 substr($res, $off, 1, $;);
698 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
699 substr($res, $off, 1, $;);
700 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
701 substr($res, $off, 1, 'X');
703 substr($res, $off, 1, $c);
707 if ($sanitise_quote eq '//') {
708 $sanitise_quote = '';
711 # The pathname on a #include may be surrounded by '<' and '>'.
712 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
713 my $clean = 'X' x length($1);
714 $res =~ s@\<.*\>@<$clean>@;
716 # The whole of a #error is a string.
717 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
718 my $clean = 'X' x length($1);
719 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
725 sub ctx_statement_block {
726 my ($linenr, $remain, $off) = @_;
727 my $line = $linenr - 1;
744 @stack = (['', 0]) if ($#stack == -1);
746 #warn "CSB: blk<$blk> remain<$remain>\n";
747 # If we are about to drop off the end, pull in more
750 for (; $remain > 0; $line++) {
751 last if (!defined $lines[$line]);
752 next if ($lines[$line] =~ /^-/);
755 $blk .= $lines[$line] . "\n";
760 # Bail if there is no further context.
761 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
765 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
771 $c = substr($blk, $off, 1);
772 $remainder = substr($blk, $off);
774 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
776 # Handle nested #if/#else.
777 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
778 push(@stack, [ $type, $level ]);
779 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
780 ($type, $level) = @{$stack[$#stack - 1]};
781 } elsif ($remainder =~ /^#\s*endif\b/) {
782 ($type, $level) = @{pop(@stack)};
785 # Statement ends at the ';' or a close '}' at the
787 if ($level == 0 && $c eq ';') {
791 # An else is really a conditional as long as its not else if
792 if ($level == 0 && $coff_set == 0 &&
793 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
794 $remainder =~ /^(else)(?:\s|{)/ &&
795 $remainder !~ /^else\s+if\b/) {
796 $coff = $off + length($1) - 1;
798 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
799 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
802 if (($type eq '' || $type eq '(') && $c eq '(') {
806 if ($type eq '(' && $c eq ')') {
808 $type = ($level != 0)? '(' : '';
810 if ($level == 0 && $coff < $soff) {
813 #warn "CSB: mark coff<$coff>\n";
816 if (($type eq '' || $type eq '{') && $c eq '{') {
820 if ($type eq '{' && $c eq '}') {
822 $type = ($level != 0)? '{' : '';
825 if (substr($blk, $off + 1, 1) eq ';') {
831 # Preprocessor commands end at the newline unless escaped.
832 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
840 # We are truly at the end, so shuffle to the next line.
847 my $statement = substr($blk, $soff, $off - $soff + 1);
848 my $condition = substr($blk, $soff, $coff - $soff + 1);
850 #warn "STATEMENT<$statement>\n";
851 #warn "CONDITION<$condition>\n";
853 #print "coff<$coff> soff<$off> loff<$loff>\n";
855 return ($statement, $condition,
856 $line, $remain + 1, $off - $loff + 1, $level);
859 sub statement_lines {
862 # Strip the diff line prefixes and rip blank lines at start and end.
863 $stmt =~ s/(^|\n)./$1/g;
867 my @stmt_lines = ($stmt =~ /\n/g);
869 return $#stmt_lines + 2;
872 sub statement_rawlines {
875 my @stmt_lines = ($stmt =~ /\n/g);
877 return $#stmt_lines + 2;
880 sub statement_block_size {
883 $stmt =~ s/(^|\n)./$1/g;
889 my @stmt_lines = ($stmt =~ /\n/g);
890 my @stmt_statements = ($stmt =~ /;/g);
892 my $stmt_lines = $#stmt_lines + 2;
893 my $stmt_statements = $#stmt_statements + 1;
895 if ($stmt_lines > $stmt_statements) {
898 return $stmt_statements;
902 sub ctx_statement_full {
903 my ($linenr, $remain, $off) = @_;
904 my ($statement, $condition, $level);
908 # Grab the first conditional/block pair.
909 ($statement, $condition, $linenr, $remain, $off, $level) =
910 ctx_statement_block($linenr, $remain, $off);
911 #print "F: c<$condition> s<$statement> remain<$remain>\n";
912 push(@chunks, [ $condition, $statement ]);
913 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
914 return ($level, $linenr, @chunks);
917 # Pull in the following conditional/block pairs and see if they
918 # could continue the statement.
920 ($statement, $condition, $linenr, $remain, $off, $level) =
921 ctx_statement_block($linenr, $remain, $off);
922 #print "C: c<$condition> s<$statement> remain<$remain>\n";
923 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
925 push(@chunks, [ $condition, $statement ]);
928 return ($level, $linenr, @chunks);
932 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
934 my $start = $linenr - 1;
941 my @stack = ($level);
942 for ($line = $start; $remain > 0; $line++) {
943 next if ($rawlines[$line] =~ /^-/);
946 $blk .= $rawlines[$line];
948 # Handle nested #if/#else.
949 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
950 push(@stack, $level);
951 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
952 $level = $stack[$#stack - 1];
953 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
954 $level = pop(@stack);
957 foreach my $c (split(//, $lines[$line])) {
958 ##print "C<$c>L<$level><$open$close>O<$off>\n";
964 if ($c eq $close && $level > 0) {
966 last if ($level == 0);
967 } elsif ($c eq $open) {
972 if (!$outer || $level <= 1) {
973 push(@res, $rawlines[$line]);
976 last if ($level == 0);
979 return ($level, @res);
981 sub ctx_block_outer {
982 my ($linenr, $remain) = @_;
984 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
988 my ($linenr, $remain) = @_;
990 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
994 my ($linenr, $remain, $off) = @_;
996 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
999 sub ctx_block_level {
1000 my ($linenr, $remain) = @_;
1002 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1004 sub ctx_statement_level {
1005 my ($linenr, $remain, $off) = @_;
1007 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1010 sub ctx_locate_comment {
1011 my ($first_line, $end_line) = @_;
1013 # Catch a comment on the end of the line itself.
1014 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1015 return $current_comment if (defined $current_comment);
1017 # Look through the context and try and figure out if there is a
1020 $current_comment = '';
1021 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1022 my $line = $rawlines[$linenr - 1];
1024 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1027 if ($line =~ m@/\*@) {
1030 if (!$in_comment && $current_comment ne '') {
1031 $current_comment = '';
1033 $current_comment .= $line . "\n" if ($in_comment);
1034 if ($line =~ m@\*/@) {
1039 chomp($current_comment);
1040 return($current_comment);
1042 sub ctx_has_comment {
1043 my ($first_line, $end_line) = @_;
1044 my $cmt = ctx_locate_comment($first_line, $end_line);
1046 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1047 ##print "CMMT: $cmt\n";
1049 return ($cmt ne '');
1053 my ($linenr, $cnt) = @_;
1055 my $offset = $linenr - 1;
1060 $line = $rawlines[$offset++];
1061 next if (defined($line) && $line =~ /^-/);
1073 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1076 $coded = sprintf("^%c", unpack('C', $2) + 64);
1085 my $av_preprocessor = 0;
1090 sub annotate_reset {
1091 $av_preprocessor = 0;
1093 @av_paren_type = ('E');
1094 $av_pend_colon = 'O';
1097 sub annotate_values {
1098 my ($stream, $type) = @_;
1101 my $var = '_' x length($stream);
1104 print "$stream\n" if ($dbg_values > 1);
1106 while (length($cur)) {
1107 @av_paren_type = ('E') if ($#av_paren_type < 0);
1108 print " <" . join('', @av_paren_type) .
1109 "> <$type> <$av_pending>" if ($dbg_values > 1);
1110 if ($cur =~ /^(\s+)/o) {
1111 print "WS($1)\n" if ($dbg_values > 1);
1112 if ($1 =~ /\n/ && $av_preprocessor) {
1113 $type = pop(@av_paren_type);
1114 $av_preprocessor = 0;
1117 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1118 print "CAST($1)\n" if ($dbg_values > 1);
1119 push(@av_paren_type, $type);
1122 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1123 print "DECLARE($1)\n" if ($dbg_values > 1);
1126 } elsif ($cur =~ /^($Modifier)\s*/) {
1127 print "MODIFIER($1)\n" if ($dbg_values > 1);
1130 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1131 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1132 $av_preprocessor = 1;
1133 push(@av_paren_type, $type);
1139 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1140 print "UNDEF($1)\n" if ($dbg_values > 1);
1141 $av_preprocessor = 1;
1142 push(@av_paren_type, $type);
1144 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1145 print "PRE_START($1)\n" if ($dbg_values > 1);
1146 $av_preprocessor = 1;
1148 push(@av_paren_type, $type);
1149 push(@av_paren_type, $type);
1152 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1153 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1154 $av_preprocessor = 1;
1156 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1160 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1161 print "PRE_END($1)\n" if ($dbg_values > 1);
1163 $av_preprocessor = 1;
1165 # Assume all arms of the conditional end as this
1166 # one does, and continue as if the #endif was not here.
1167 pop(@av_paren_type);
1168 push(@av_paren_type, $type);
1171 } elsif ($cur =~ /^(\\\n)/o) {
1172 print "PRECONT($1)\n" if ($dbg_values > 1);
1174 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1175 print "ATTR($1)\n" if ($dbg_values > 1);
1176 $av_pending = $type;
1179 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1180 print "SIZEOF($1)\n" if ($dbg_values > 1);
1186 } elsif ($cur =~ /^(if|while|for)\b/o) {
1187 print "COND($1)\n" if ($dbg_values > 1);
1191 } elsif ($cur =~/^(case)/o) {
1192 print "CASE($1)\n" if ($dbg_values > 1);
1193 $av_pend_colon = 'C';
1196 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1197 print "KEYWORD($1)\n" if ($dbg_values > 1);
1200 } elsif ($cur =~ /^(\()/o) {
1201 print "PAREN('$1')\n" if ($dbg_values > 1);
1202 push(@av_paren_type, $av_pending);
1206 } elsif ($cur =~ /^(\))/o) {
1207 my $new_type = pop(@av_paren_type);
1208 if ($new_type ne '_') {
1210 print "PAREN('$1') -> $type\n"
1211 if ($dbg_values > 1);
1213 print "PAREN('$1')\n" if ($dbg_values > 1);
1216 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1217 print "FUNC($1)\n" if ($dbg_values > 1);
1221 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1222 if (defined $2 && $type eq 'C' || $type eq 'T') {
1223 $av_pend_colon = 'B';
1224 } elsif ($type eq 'E') {
1225 $av_pend_colon = 'L';
1227 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1230 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1231 print "IDENT($1)\n" if ($dbg_values > 1);
1234 } elsif ($cur =~ /^($Assignment)/o) {
1235 print "ASSIGN($1)\n" if ($dbg_values > 1);
1238 } elsif ($cur =~/^(;|{|})/) {
1239 print "END($1)\n" if ($dbg_values > 1);
1241 $av_pend_colon = 'O';
1243 } elsif ($cur =~/^(,)/) {
1244 print "COMMA($1)\n" if ($dbg_values > 1);
1247 } elsif ($cur =~ /^(\?)/o) {
1248 print "QUESTION($1)\n" if ($dbg_values > 1);
1251 } elsif ($cur =~ /^(:)/o) {
1252 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1254 substr($var, length($res), 1, $av_pend_colon);
1255 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1260 $av_pend_colon = 'O';
1262 } elsif ($cur =~ /^(\[)/o) {
1263 print "CLOSE($1)\n" if ($dbg_values > 1);
1266 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1269 print "OPV($1)\n" if ($dbg_values > 1);
1276 substr($var, length($res), 1, $variant);
1279 } elsif ($cur =~ /^($Operators)/o) {
1280 print "OP($1)\n" if ($dbg_values > 1);
1281 if ($1 ne '++' && $1 ne '--') {
1285 } elsif ($cur =~ /(^.)/o) {
1286 print "C($1)\n" if ($dbg_values > 1);
1289 $cur = substr($cur, length($1));
1290 $res .= $type x length($1);
1294 return ($res, $var);
1298 my ($possible, $line) = @_;
1299 my $notPermitted = qr{(?:
1316 ^(?:typedef|struct|enum)\b
1318 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1319 if ($possible !~ $notPermitted) {
1320 # Check for modifiers.
1321 $possible =~ s/\s*$Storage\s*//g;
1322 $possible =~ s/\s*$Sparse\s*//g;
1323 if ($possible =~ /^\s*$/) {
1325 } elsif ($possible =~ /\s/) {
1326 $possible =~ s/\s*$Type\s*//g;
1327 for my $modifier (split(' ', $possible)) {
1328 if ($modifier !~ $notPermitted) {
1329 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1330 push(@modifierList, $modifier);
1335 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1336 push(@typeList, $possible);
1340 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1347 return !defined $ignore_type{$_[0]};
1351 if (!show_type($_[1]) ||
1352 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1357 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1359 $line = "$prefix$_[0]: $_[2]\n";
1361 $line = (split('\n', $line))[0] . "\n" if ($terse);
1363 push(our @report, $line);
1372 if (report("ERROR", $_[0], $_[1])) {
1378 if (report("WARNING", $_[0], $_[1])) {
1384 if ($check && report("CHECK", $_[0], $_[1])) {
1390 sub check_absolute_file {
1391 my ($absolute, $herecurr) = @_;
1392 my $file = $absolute;
1394 ##print "absolute<$absolute>\n";
1396 # See if any suffix of this path is a path within the tree.
1397 while ($file =~ s@^[^/]*/@@) {
1398 if (-f "$root/$file") {
1399 ##print "file<$file>\n";
1407 # It is, so see if the prefix is acceptable.
1408 my $prefix = $absolute;
1409 substr($prefix, -length($file)) = '';
1411 ##print "prefix<$prefix>\n";
1412 if ($prefix ne ".../") {
1413 WARN("USE_RELATIVE_PATH",
1414 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1419 my $filename = shift;
1425 my $stashrawline="";
1436 my $in_header_lines = 1;
1437 my $in_commit_log = 0; #Scanning lines before patch
1445 # Trace the real file/line as we go.
1451 my $comment_edge = 0;
1455 my $prev_values = 'E';
1458 my %suppress_ifbraces;
1459 my %suppress_whiletrailers;
1460 my %suppress_export;
1461 my $suppress_statement = 0;
1463 # Pre-scan the patch sanitizing the lines.
1464 # Pre-scan the patch looking for any __setup documentation.
1466 my @setup_docs = ();
1469 sanitise_line_reset();
1471 foreach my $rawline (@rawlines) {
1475 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1477 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1482 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1491 # Guestimate if this is a continuing comment. Run
1492 # the context looking for a comment "edge". If this
1493 # edge is a close comment then we must be in a comment
1497 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1498 next if (defined $rawlines[$ln - 1] &&
1499 $rawlines[$ln - 1] =~ /^-/);
1501 #print "RAW<$rawlines[$ln - 1]>\n";
1502 last if (!defined $rawlines[$ln - 1]);
1503 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1504 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1509 if (defined $edge && $edge eq '*/') {
1513 # Guestimate if this is a continuing comment. If this
1514 # is the start of a diff block and this line starts
1515 # ' *' then it is very likely a comment.
1516 if (!defined $edge &&
1517 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1522 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1523 sanitise_line_reset($in_comment);
1525 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1526 # Standardise the strings and chars within the input to
1527 # simplify matching -- only bother with positive lines.
1528 $line = sanitise_line($rawline);
1530 push(@lines, $line);
1533 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1538 #print "==>$rawline\n";
1539 #print "-->$line\n";
1541 if ($setup_docs && $line =~ /^\+/) {
1542 push(@setup_docs, $line);
1550 foreach my $line (@lines) {
1553 my $rawline = $rawlines[$linenr - 1];
1555 #extract the line range in the file after the patch is applied
1556 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1558 $first_line = $linenr + 1;
1568 %suppress_ifbraces = ();
1569 %suppress_whiletrailers = ();
1570 %suppress_export = ();
1571 $suppress_statement = 0;
1574 # track the line number as we move through the hunk, note that
1575 # new versions of GNU diff omit the leading space on completely
1576 # blank context lines so we need to count that too.
1577 } elsif ($line =~ /^( |\+|$)/) {
1579 $realcnt-- if ($realcnt != 0);
1581 # Measure the line length and indent.
1582 ($length, $indent) = line_stats($rawline);
1584 # Track the previous line.
1585 ($prevline, $stashline) = ($stashline, $line);
1586 ($previndent, $stashindent) = ($stashindent, $indent);
1587 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1589 #warn "line<$line>\n";
1591 } elsif ($realcnt == 1) {
1595 my $hunk_line = ($realcnt != 0);
1597 #make up the handle for any error we report on this line
1598 $prefix = "$filename:$realline: " if ($emacs && $file);
1599 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1601 $here = "#$linenr: " if (!$file);
1602 $here = "#$realline: " if ($file);
1604 # extract the filename as it passes
1605 if ($line =~ /^diff --git.*?(\S+)$/) {
1607 $realfile =~ s@^([^/]*)/@@;
1609 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1611 $realfile =~ s@^([^/]*)/@@;
1615 if (!$file && $tree && $p1_prefix ne '' &&
1616 -e "$root/$p1_prefix") {
1617 WARN("PATCH_PREFIX",
1618 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1621 if ($realfile =~ m@^include/asm/@) {
1622 ERROR("MODIFIED_INCLUDE_ASM",
1623 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1628 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1630 my $hereline = "$here\n$rawline\n";
1631 my $herecurr = "$here\n$rawline\n";
1632 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1634 $cnt_lines++ if ($realcnt != 0);
1636 # Check for incorrect file permissions
1637 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1638 my $permhere = $here . "FILE: $realfile\n";
1639 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1640 ERROR("EXECUTE_PERMISSIONS",
1641 "do not set execute permissions for source files\n" . $permhere);
1645 # Check the patch for a signoff:
1646 if ($line =~ /^\s*signed-off-by:/i) {
1651 # Check signature styles
1652 if (!$in_header_lines &&
1653 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1654 my $space_before = $1;
1656 my $space_after = $3;
1658 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1660 if (defined $space_before && $space_before ne "") {
1661 WARN("BAD_SIGN_OFF",
1662 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1664 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1665 WARN("BAD_SIGN_OFF",
1666 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1668 if (!defined $space_after || $space_after ne " ") {
1669 WARN("BAD_SIGN_OFF",
1670 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
1673 my ($email_name, $email_address, $comment) = parse_email($email);
1674 my $suggested_email = format_email(($email_name, $email_address));
1675 if ($suggested_email eq "") {
1676 ERROR("BAD_SIGN_OFF",
1677 "Unrecognized email address: '$email'\n" . $herecurr);
1679 my $dequoted = $suggested_email;
1680 $dequoted =~ s/^"//;
1681 $dequoted =~ s/" </ </;
1682 # Don't force email to have quotes
1683 # Allow just an angle bracketed address
1684 if ("$dequoted$comment" ne $email &&
1685 "<$email_address>$comment" ne $email &&
1686 "$suggested_email$comment" ne $email) {
1687 WARN("BAD_SIGN_OFF",
1688 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1693 # Check for wrappage within a valid hunk of the file
1694 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1695 ERROR("CORRUPTED_PATCH",
1696 "patch seems to be corrupt (line wrapped?)\n" .
1697 $herecurr) if (!$emitted_corrupt++);
1700 # Check for absolute kernel paths.
1702 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1705 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1706 check_absolute_file($1, $herecurr)) {
1709 check_absolute_file($file, $herecurr);
1714 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1715 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1716 $rawline !~ m/^$UTF8*$/) {
1717 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1719 my $blank = copy_spacing($rawline);
1720 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1721 my $hereptr = "$hereline$ptr\n";
1724 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1727 # Check if it's the start of a commit log
1728 # (not a header line and we haven't seen the patch filename)
1729 if ($in_header_lines && $realfile =~ /^$/ &&
1730 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1731 $in_header_lines = 0;
1735 # Still not yet in a patch, check for any UTF-8
1736 if ($in_commit_log && $realfile =~ /^$/ &&
1737 $rawline =~ /$NON_ASCII_UTF8/) {
1738 CHK("UTF8_BEFORE_PATCH",
1739 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1742 # ignore non-hunk lines and lines being removed
1743 next if (!$hunk_line || $line =~ /^-/);
1745 #trailing whitespace
1746 if ($line =~ /^\+.*\015/) {
1747 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1748 ERROR("DOS_LINE_ENDINGS",
1749 "DOS line endings\n" . $herevet);
1751 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1752 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1753 ERROR("TRAILING_WHITESPACE",
1754 "trailing whitespace\n" . $herevet);
1758 # check for Kconfig help text having a real description
1759 # Only applies when adding the entry originally, after that we do not have
1760 # sufficient context to determine whether it is indeed long enough.
1761 if ($realfile =~ /Kconfig/ &&
1762 $line =~ /.\s*config\s+/) {
1765 my $ln = $linenr + 1;
1769 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1770 $f = $lines[$ln - 1];
1771 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1772 $is_end = $lines[$ln - 1] =~ /^\+/;
1774 next if ($f =~ /^-/);
1776 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1778 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1785 next if ($f =~ /^$/);
1786 if ($f =~ /^\s*config\s/) {
1792 WARN("CONFIG_DESCRIPTION",
1793 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1794 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1797 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1798 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1801 'EXTRA_AFLAGS' => 'asflags-y',
1802 'EXTRA_CFLAGS' => 'ccflags-y',
1803 'EXTRA_CPPFLAGS' => 'cppflags-y',
1804 'EXTRA_LDFLAGS' => 'ldflags-y',
1807 WARN("DEPRECATED_VARIABLE",
1808 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1811 # check we are in a valid source file if not then ignore this hunk
1812 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1815 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1816 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1817 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1818 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1822 "line over 80 characters\n" . $herecurr);
1825 # check for spaces before a quoted newline
1826 if ($rawline =~ /^.*\".*\s\\n/) {
1827 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1828 "unnecessary whitespace before a quoted newline\n" . $herecurr);
1831 # check for adding lines without a newline.
1832 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1833 WARN("MISSING_EOF_NEWLINE",
1834 "adding a line without newline at end of file\n" . $herecurr);
1837 # Blackfin: use hi/lo macros
1838 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1839 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1840 my $herevet = "$here\n" . cat_vet($line) . "\n";
1842 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1844 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1845 my $herevet = "$here\n" . cat_vet($line) . "\n";
1847 "use the HI() macro, not (... >> 16)\n" . $herevet);
1851 # check we are in a valid source file C or perl if not then ignore this hunk
1852 next if ($realfile !~ /\.(h|c|pl)$/);
1854 # at the beginning of a line any tabs must come first and anything
1855 # more than 8 must use tabs.
1856 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1857 $rawline =~ /^\+\s* \s*/) {
1858 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1859 ERROR("CODE_INDENT",
1860 "code indent should use tabs where possible\n" . $herevet);
1864 # check for space before tabs.
1865 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1866 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1867 WARN("SPACE_BEFORE_TAB",
1868 "please, no space before tabs\n" . $herevet);
1871 # check for spaces at the beginning of a line.
1873 # 1) within comments
1874 # 2) indented preprocessor commands
1876 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1877 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1878 WARN("LEADING_SPACE",
1879 "please, no spaces at the start of a line\n" . $herevet);
1882 # check we are in a valid C source file if not then ignore this hunk
1883 next if ($realfile !~ /\.(h|c)$/);
1885 # check for RCS/CVS revision markers
1886 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1888 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1891 # Blackfin: don't use __builtin_bfin_[cs]sync
1892 if ($line =~ /__builtin_bfin_csync/) {
1893 my $herevet = "$here\n" . cat_vet($line) . "\n";
1895 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1897 if ($line =~ /__builtin_bfin_ssync/) {
1898 my $herevet = "$here\n" . cat_vet($line) . "\n";
1900 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1903 # Check for potential 'bare' types
1904 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1906 #print "LINE<$line>\n";
1907 if ($linenr >= $suppress_statement &&
1908 $realcnt && $line =~ /.\s*\S/) {
1909 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1910 ctx_statement_block($linenr, $realcnt, 0);
1911 $stat =~ s/\n./\n /g;
1912 $cond =~ s/\n./\n /g;
1914 #print "linenr<$linenr> <$stat>\n";
1915 # If this statement has no statement boundaries within
1916 # it there is no point in retrying a statement scan
1917 # until we hit end of it.
1918 my $frag = $stat; $frag =~ s/;+\s*$//;
1919 if ($frag !~ /(?:{|;)/) {
1920 #print "skip<$line_nr_next>\n";
1921 $suppress_statement = $line_nr_next;
1924 # Find the real next line.
1925 $realline_next = $line_nr_next;
1926 if (defined $realline_next &&
1927 (!defined $lines[$realline_next - 1] ||
1928 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1935 # Ignore goto labels.
1936 if ($s =~ /$Ident:\*$/s) {
1938 # Ignore functions being called
1939 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1941 } elsif ($s =~ /^.\s*else\b/s) {
1943 # declarations always start with types
1944 } 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) {
1947 possible($type, "A:" . $s);
1949 # definitions in global scope can only start with types
1950 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1951 possible($1, "B:" . $s);
1954 # any (foo ... *) is a pointer cast, and foo is a type
1955 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1956 possible($1, "C:" . $s);
1959 # Check for any sort of function declaration.
1960 # int foo(something bar, other baz);
1961 # void (*store_gdt)(x86_descr_ptr *);
1962 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1963 my ($name_len) = length($1);
1966 substr($ctx, 0, $name_len + 1, '');
1967 $ctx =~ s/\)[^\)]*$//;
1969 for my $arg (split(/\s*,\s*/, $ctx)) {
1970 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1972 possible($1, "D:" . $s);
1980 # Checks which may be anchored in the context.
1983 # Check for switch () and associated case and default
1984 # statements should be at the same indent.
1985 if ($line=~/\bswitch\s*\(.*\)/) {
1988 my @ctx = ctx_block_outer($linenr, $realcnt);
1990 for my $ctx (@ctx) {
1991 my ($clen, $cindent) = line_stats($ctx);
1992 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1993 $indent != $cindent) {
1994 $err .= "$sep$ctx\n";
2001 ERROR("SWITCH_CASE_INDENT_LEVEL",
2002 "switch and case should be at the same indent\n$hereline$err");
2006 # if/while/etc brace do not go on next line, unless defining a do while loop,
2007 # or if that brace on the next line is for something else
2008 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2009 my $pre_ctx = "$1$2";
2011 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2013 if ($line =~ /^\+\t{6,}/) {
2014 WARN("DEEP_INDENTATION",
2015 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2018 my $ctx_cnt = $realcnt - $#ctx - 1;
2019 my $ctx = join("\n", @ctx);
2021 my $ctx_ln = $linenr;
2022 my $ctx_skip = $realcnt;
2024 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2025 defined $lines[$ctx_ln - 1] &&
2026 $lines[$ctx_ln - 1] =~ /^-/)) {
2027 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2028 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2032 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2033 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2035 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2037 "that open brace { should be on the previous line\n" .
2038 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2040 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2041 $ctx =~ /\)\s*\;\s*$/ &&
2042 defined $lines[$ctx_ln - 1])
2044 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2045 if ($nindent > $indent) {
2046 WARN("TRAILING_SEMICOLON",
2047 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2048 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2053 # Check relative indent for conditionals and blocks.
2054 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2055 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2056 ctx_statement_block($linenr, $realcnt, 0)
2057 if (!defined $stat);
2058 my ($s, $c) = ($stat, $cond);
2060 substr($s, 0, length($c), '');
2062 # Make sure we remove the line prefixes as we have
2063 # none on the first line, and are going to readd them
2067 # Find out how long the conditional actually is.
2068 my @newlines = ($c =~ /\n/gs);
2069 my $cond_lines = 1 + $#newlines;
2071 # We want to check the first line inside the block
2072 # starting at the end of the conditional, so remove:
2073 # 1) any blank line termination
2074 # 2) any opening brace { on end of the line
2076 my $continuation = 0;
2078 $s =~ s/^.*\bdo\b//;
2080 if ($s =~ s/^\s*\\//) {
2083 if ($s =~ s/^\s*?\n//) {
2088 # Also ignore a loop construct at the end of a
2089 # preprocessor statement.
2090 if (($prevline =~ /^.\s*#\s*define\s/ ||
2091 $prevline =~ /\\\s*$/) && $continuation == 0) {
2097 while ($cond_ptr != $cond_lines) {
2098 $cond_ptr = $cond_lines;
2100 # If we see an #else/#elif then the code
2102 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2107 # 1) blank lines, they should be at 0,
2108 # 2) preprocessor lines, and
2110 if ($continuation ||
2112 $s =~ /^\s*#\s*?/ ||
2113 $s =~ /^\s*$Ident\s*:/) {
2114 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2115 if ($s =~ s/^.*?\n//) {
2121 my (undef, $sindent) = line_stats("+" . $s);
2122 my $stat_real = raw_line($linenr, $cond_lines);
2124 # Check if either of these lines are modified, else
2125 # this is not this patch's fault.
2126 if (!defined($stat_real) ||
2127 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2130 if (defined($stat_real) && $cond_lines > 1) {
2131 $stat_real = "[...]\n$stat_real";
2134 #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";
2136 if ($check && (($sindent % 8) != 0 ||
2137 ($sindent <= $indent && $s ne ''))) {
2138 WARN("SUSPECT_CODE_INDENT",
2139 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2143 # Track the 'values' across context and added lines.
2144 my $opline = $line; $opline =~ s/^./ /;
2145 my ($curr_values, $curr_vars) =
2146 annotate_values($opline . "\n", $prev_values);
2147 $curr_values = $prev_values . $curr_values;
2149 my $outline = $opline; $outline =~ s/\t/ /g;
2150 print "$linenr > .$outline\n";
2151 print "$linenr > $curr_values\n";
2152 print "$linenr > $curr_vars\n";
2154 $prev_values = substr($curr_values, -1);
2156 #ignore lines not being added
2157 if ($line=~/^[^\+]/) {next;}
2159 # TEST: allow direct testing of the type matcher.
2161 if ($line =~ /^.\s*$Declare\s*$/) {
2163 "TEST: is type\n" . $herecurr);
2164 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2165 ERROR("TEST_NOT_TYPE",
2166 "TEST: is not type ($1 is)\n". $herecurr);
2170 # TEST: allow direct testing of the attribute matcher.
2172 if ($line =~ /^.\s*$Modifier\s*$/) {
2174 "TEST: is attr\n" . $herecurr);
2175 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2176 ERROR("TEST_NOT_ATTR",
2177 "TEST: is not attr ($1 is)\n". $herecurr);
2182 # check for initialisation to aggregates open brace on the next line
2183 if ($line =~ /^.\s*{/ &&
2184 $prevline =~ /(?:^|[^=])=\s*$/) {
2186 "that open brace { should be on the previous line\n" . $hereprev);
2190 # Checks which are anchored on the added line.
2193 # check for malformed paths in #include statements (uses RAW line)
2194 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2196 if ($path =~ m{//}) {
2197 ERROR("MALFORMED_INCLUDE",
2198 "malformed #include filename\n" .
2203 # no C99 // comments
2204 if ($line =~ m{//}) {
2205 ERROR("C99_COMMENTS",
2206 "do not use C99 // comments\n" . $herecurr);
2208 # Remove C99 comments.
2210 $opline =~ s@//.*@@;
2212 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2213 # the whole statement.
2214 #print "APW <$lines[$realline_next - 1]>\n";
2215 if (defined $realline_next &&
2216 exists $lines[$realline_next - 1] &&
2217 !defined $suppress_export{$realline_next} &&
2218 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2219 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2220 # Handle definitions which produce identifiers with
2223 # EXPORT_SYMBOL(something_foo);
2225 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2226 $name =~ /^${Ident}_$2/) {
2227 #print "FOO C name<$name>\n";
2228 $suppress_export{$realline_next} = 1;
2230 } elsif ($stat !~ /(?:
2232 ^.DEFINE_$Ident\(\Q$name\E\)|
2233 ^.DECLARE_$Ident\(\Q$name\E\)|
2234 ^.LIST_HEAD\(\Q$name\E\)|
2235 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2236 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2238 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2239 $suppress_export{$realline_next} = 2;
2241 $suppress_export{$realline_next} = 1;
2244 if (!defined $suppress_export{$linenr} &&
2245 $prevline =~ /^.\s*$/ &&
2246 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2247 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2248 #print "FOO B <$lines[$linenr - 1]>\n";
2249 $suppress_export{$linenr} = 2;
2251 if (defined $suppress_export{$linenr} &&
2252 $suppress_export{$linenr} == 2) {
2253 WARN("EXPORT_SYMBOL",
2254 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2257 # check for global initialisers.
2258 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2259 ERROR("GLOBAL_INITIALISERS",
2260 "do not initialise globals to 0 or NULL\n" .
2263 # check for static initialisers.
2264 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2265 ERROR("INITIALISED_STATIC",
2266 "do not initialise statics to 0 or NULL\n" .
2270 # check for static const char * arrays.
2271 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2272 WARN("STATIC_CONST_CHAR_ARRAY",
2273 "static const char * array should probably be static const char * const\n" .
2277 # check for static char foo[] = "bar" declarations.
2278 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2279 WARN("STATIC_CONST_CHAR_ARRAY",
2280 "static char array declaration should probably be static const char\n" .
2284 # check for declarations of struct pci_device_id
2285 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2286 WARN("DEFINE_PCI_DEVICE_TABLE",
2287 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2290 # check for new typedefs, only function parameters and sparse annotations
2292 if ($line =~ /\btypedef\s/ &&
2293 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2294 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2295 $line !~ /\b$typeTypedefs\b/ &&
2296 $line !~ /\b__bitwise(?:__|)\b/) {
2297 WARN("NEW_TYPEDEFS",
2298 "do not add new typedefs\n" . $herecurr);
2301 # * goes on variable not on type
2303 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2305 my ($from, $to) = ($2, $2);
2307 # Should start with a space.
2308 $to =~ s/^(\S)/ $1/;
2309 # Should not end with a space.
2311 # '*'s should not have spaces between.
2312 while ($to =~ s/\*\s+\*/\*\*/) {
2315 #print "from<$from> to<$to>\n";
2317 ERROR("POINTER_LOCATION",
2318 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2321 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2323 my ($from, $to, $ident) = ($2, $2, $3);
2325 # Should start with a space.
2326 $to =~ s/^(\S)/ $1/;
2327 # Should not end with a space.
2329 # '*'s should not have spaces between.
2330 while ($to =~ s/\*\s+\*/\*\*/) {
2332 # Modifiers should have spaces.
2333 $to =~ s/(\b$Modifier$)/$1 /;
2335 #print "from<$from> to<$to> ident<$ident>\n";
2336 if ($from ne $to && $ident !~ /^$Modifier$/) {
2337 ERROR("POINTER_LOCATION",
2338 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2342 # # no BUG() or BUG_ON()
2343 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
2344 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2345 # print "$herecurr";
2349 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2350 WARN("LINUX_VERSION_CODE",
2351 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2354 # check for uses of printk_ratelimit
2355 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2356 WARN("PRINTK_RATELIMITED",
2357 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2360 # printk should use KERN_* levels. Note that follow on printk's on the
2361 # same line do not need a level, so we use the current block context
2362 # to try and find and validate the current printk. In summary the current
2363 # printk includes all preceding printk's which have no newline on the end.
2364 # we assume the first bad printk is the one to report.
2365 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2367 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2368 #print "CHECK<$lines[$ln - 1]\n";
2369 # we have a preceding printk if it ends
2370 # with "\n" ignore it, else it is to blame
2371 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2372 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2379 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2380 "printk() should include KERN_ facility level\n" . $herecurr);
2384 # function brace can't be on same line, except for #defines of do while,
2385 # or if closed on same line
2386 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2387 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2389 "open brace '{' following function declarations go on the next line\n" . $herecurr);
2392 # open braces for enum, union and struct go on the same line.
2393 if ($line =~ /^.\s*{/ &&
2394 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2396 "open brace '{' following $1 go on the same line\n" . $hereprev);
2399 # missing space after union, struct or enum definition
2400 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2402 "missing space after $1 definition\n" . $herecurr);
2405 # check for spacing round square brackets; allowed:
2406 # 1. with a type on the left -- int [] a;
2407 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2408 # 3. inside a curly brace -- = { [0...10] = 5 }
2409 while ($line =~ /(.*?\s)\[/g) {
2410 my ($where, $prefix) = ($-[1], $1);
2411 if ($prefix !~ /$Type\s+$/ &&
2412 ($where != 0 || $prefix !~ /^.\s+$/) &&
2413 $prefix !~ /{\s+$/) {
2414 ERROR("BRACKET_SPACE",
2415 "space prohibited before open square bracket '['\n" . $herecurr);
2419 # check for spaces between functions and their parentheses.
2420 while ($line =~ /($Ident)\s+\(/g) {
2422 my $ctx_before = substr($line, 0, $-[1]);
2423 my $ctx = "$ctx_before$name";
2425 # Ignore those directives where spaces _are_ permitted.
2427 if|for|while|switch|return|case|
2428 volatile|__volatile__|
2429 __attribute__|format|__extension__|
2433 # cpp #define statements have non-optional spaces, ie
2434 # if there is a space between the name and the open
2435 # parenthesis it is simply not a parameter group.
2436 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2438 # cpp #elif statement condition may start with a (
2439 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2441 # If this whole things ends with a type its most
2442 # likely a typedef for a function.
2443 } elsif ($ctx =~ /$Type$/) {
2447 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2450 # Check operator spacing.
2451 if (!($line=~/\#\s*include/)) {
2453 <<=|>>=|<=|>=|==|!=|
2454 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2455 =>|->|<<|>>|<|>|=|!|~|
2456 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2459 my @elements = split(/($ops|;)/, $opline);
2462 my $blank = copy_spacing($opline);
2464 for (my $n = 0; $n < $#elements; $n += 2) {
2465 $off += length($elements[$n]);
2467 # Pick up the preceding and succeeding characters.
2468 my $ca = substr($opline, 0, $off);
2470 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2471 $cc = substr($opline, $off + length($elements[$n + 1]));
2473 my $cb = "$ca$;$cc";
2476 $a = 'V' if ($elements[$n] ne '');
2477 $a = 'W' if ($elements[$n] =~ /\s$/);
2478 $a = 'C' if ($elements[$n] =~ /$;$/);
2479 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2480 $a = 'O' if ($elements[$n] eq '');
2481 $a = 'E' if ($ca =~ /^\s*$/);
2483 my $op = $elements[$n + 1];
2486 if (defined $elements[$n + 2]) {
2487 $c = 'V' if ($elements[$n + 2] ne '');
2488 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2489 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2490 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2491 $c = 'O' if ($elements[$n + 2] eq '');
2492 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2497 my $ctx = "${a}x${c}";
2499 my $at = "(ctx:$ctx)";
2501 my $ptr = substr($blank, 0, $off) . "^";
2502 my $hereptr = "$hereline$ptr\n";
2504 # Pull out the value of this operator.
2505 my $op_type = substr($curr_values, $off + 1, 1);
2507 # Get the full operator variant.
2508 my $opv = $op . substr($curr_vars, $off, 1);
2510 # Ignore operators passed as parameters.
2511 if ($op_type ne 'V' &&
2512 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2515 # } elsif ($op =~ /^$;+$/) {
2517 # ; should have either the end of line or a space or \ after it
2518 } elsif ($op eq ';') {
2519 if ($ctx !~ /.x[WEBC]/ &&
2520 $cc !~ /^\\/ && $cc !~ /^;/) {
2522 "space required after that '$op' $at\n" . $hereptr);
2526 } elsif ($op eq '//') {
2530 # : when part of a bitfield
2531 } elsif ($op eq '->' || $opv eq ':B') {
2532 if ($ctx =~ /Wx.|.xW/) {
2534 "spaces prohibited around that '$op' $at\n" . $hereptr);
2537 # , must have a space on the right.
2538 } elsif ($op eq ',') {
2539 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2541 "space required after that '$op' $at\n" . $hereptr);
2544 # '*' as part of a type definition -- reported already.
2545 } elsif ($opv eq '*_') {
2546 #warn "'*' is part of type\n";
2548 # unary operators should have a space before and
2549 # none after. May be left adjacent to another
2550 # unary operator, or a cast
2551 } elsif ($op eq '!' || $op eq '~' ||
2552 $opv eq '*U' || $opv eq '-U' ||
2553 $opv eq '&U' || $opv eq '&&U') {
2554 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2556 "space required before that '$op' $at\n" . $hereptr);
2558 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2559 # A unary '*' may be const
2561 } elsif ($ctx =~ /.xW/) {
2563 "space prohibited after that '$op' $at\n" . $hereptr);
2566 # unary ++ and unary -- are allowed no space on one side.
2567 } elsif ($op eq '++' or $op eq '--') {
2568 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2570 "space required one side of that '$op' $at\n" . $hereptr);
2572 if ($ctx =~ /Wx[BE]/ ||
2573 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2575 "space prohibited before that '$op' $at\n" . $hereptr);
2577 if ($ctx =~ /ExW/) {
2579 "space prohibited after that '$op' $at\n" . $hereptr);
2583 # << and >> may either have or not have spaces both sides
2584 } elsif ($op eq '<<' or $op eq '>>' or
2585 $op eq '&' or $op eq '^' or $op eq '|' or
2586 $op eq '+' or $op eq '-' or
2587 $op eq '*' or $op eq '/' or
2590 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2592 "need consistent spacing around '$op' $at\n" .
2596 # A colon needs no spaces before when it is
2597 # terminating a case value or a label.
2598 } elsif ($opv eq ':C' || $opv eq ':L') {
2599 if ($ctx =~ /Wx./) {
2601 "space prohibited before that '$op' $at\n" . $hereptr);
2604 # All the others need spaces both sides.
2605 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2608 # Ignore email addresses <foo@bar>
2610 $cc =~ /^\S+\@\S+>/) ||
2612 $ca =~ /<\S+\@\S+$/))
2618 if (($opv eq ':O' && $ca =~ /\?$/) ||
2619 ($op eq '?' && $cc =~ /^:/)) {
2625 "spaces required around that '$op' $at\n" . $hereptr);
2628 $off += length($elements[$n + 1]);
2632 # check for multiple assignments
2633 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2634 CHK("MULTIPLE_ASSIGNMENTS",
2635 "multiple assignments should be avoided\n" . $herecurr);
2638 ## # check for multiple declarations, allowing for a function declaration
2640 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2641 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2643 ## # Remove any bracketed sections to ensure we do not
2644 ## # falsly report the parameters of functions.
2646 ## while ($ln =~ s/\([^\(\)]*\)//g) {
2648 ## if ($ln =~ /,/) {
2649 ## WARN("MULTIPLE_DECLARATION",
2650 ## "declaring multiple variables together should be avoided\n" . $herecurr);
2654 #need space before brace following if, while, etc
2655 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2658 "space required before the open brace '{'\n" . $herecurr);
2661 # closing brace should have a space following it when it has anything
2663 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2665 "space required after that close brace '}'\n" . $herecurr);
2668 # check spacing on square brackets
2669 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2671 "space prohibited after that open square bracket '['\n" . $herecurr);
2673 if ($line =~ /\s\]/) {
2675 "space prohibited before that close square bracket ']'\n" . $herecurr);
2678 # check spacing on parentheses
2679 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2680 $line !~ /for\s*\(\s+;/) {
2682 "space prohibited after that open parenthesis '('\n" . $herecurr);
2684 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2685 $line !~ /for\s*\(.*;\s+\)/ &&
2686 $line !~ /:\s+\)/) {
2688 "space prohibited before that close parenthesis ')'\n" . $herecurr);
2691 #goto labels aren't indented, allow a single space however
2692 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2693 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2694 WARN("INDENTED_LABEL",
2695 "labels should not be indented\n" . $herecurr);
2698 # Return is not a function.
2699 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2703 # Flatten any parentheses
2704 $value =~ s/\(/ \(/g;
2705 $value =~ s/\)/\) /g;
2706 while ($value =~ s/\[[^\[\]]*\]/1/ ||
2707 $value !~ /(?:$Ident|-?$Constant)\s*
2709 (?:$Ident|-?$Constant)/x &&
2710 $value =~ s/\([^\(\)]*\)/1/) {
2712 #print "value<$value>\n";
2713 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2714 ERROR("RETURN_PARENTHESES",
2715 "return is not a function, parentheses are not required\n" . $herecurr);
2717 } elsif ($spacing !~ /\s+/) {
2719 "space required before the open parenthesis '('\n" . $herecurr);
2722 # Return of what appears to be an errno should normally be -'ve
2723 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2725 if ($name ne 'EOF' && $name ne 'ERROR') {
2726 WARN("USE_NEGATIVE_ERRNO",
2727 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2731 # Need a space before open parenthesis after if, while etc
2732 if ($line=~/\b(if|while|for|switch)\(/) {
2733 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
2736 # Check for illegal assignment in if conditional -- and check for trailing
2737 # statements after the conditional.
2738 if ($line =~ /do\s*(?!{)/) {
2739 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2740 ctx_statement_block($linenr, $realcnt, 0)
2741 if (!defined $stat);
2742 my ($stat_next) = ctx_statement_block($line_nr_next,
2743 $remain_next, $off_next);
2744 $stat_next =~ s/\n./\n /g;
2745 ##print "stat<$stat> stat_next<$stat_next>\n";
2747 if ($stat_next =~ /^\s*while\b/) {
2748 # If the statement carries leading newlines,
2749 # then count those as offsets.
2751 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2753 statement_rawlines($whitespace) - 1;
2755 $suppress_whiletrailers{$line_nr_next +
2759 if (!defined $suppress_whiletrailers{$linenr} &&
2760 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2761 my ($s, $c) = ($stat, $cond);
2763 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2764 ERROR("ASSIGN_IN_IF",
2765 "do not use assignment in if condition\n" . $herecurr);
2768 # Find out what is on the end of the line after the
2770 substr($s, 0, length($c), '');
2772 $s =~ s/$;//g; # Remove any comments
2773 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2774 $c !~ /}\s*while\s*/)
2776 # Find out how long the conditional actually is.
2777 my @newlines = ($c =~ /\n/gs);
2778 my $cond_lines = 1 + $#newlines;
2781 $stat_real = raw_line($linenr, $cond_lines)
2782 . "\n" if ($cond_lines);
2783 if (defined($stat_real) && $cond_lines > 1) {
2784 $stat_real = "[...]\n$stat_real";
2787 ERROR("TRAILING_STATEMENTS",
2788 "trailing statements should be on next line\n" . $herecurr . $stat_real);
2792 # Check for bitwise tests written as boolean
2804 WARN("HEXADECIMAL_BOOLEAN_TEST",
2805 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2808 # if and else should not have general statements after it
2809 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2811 $s =~ s/$;//g; # Remove any comments
2812 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2813 ERROR("TRAILING_STATEMENTS",
2814 "trailing statements should be on next line\n" . $herecurr);
2817 # if should not continue a brace
2818 if ($line =~ /}\s*if\b/) {
2819 ERROR("TRAILING_STATEMENTS",
2820 "trailing statements should be on next line\n" .
2823 # case and default should not have general statements after them
2824 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2826 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2830 ERROR("TRAILING_STATEMENTS",
2831 "trailing statements should be on next line\n" . $herecurr);
2834 # Check for }<nl>else {, these must be at the same
2835 # indent level to be relevant to each other.
2836 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2837 $previndent == $indent) {
2838 ERROR("ELSE_AFTER_BRACE",
2839 "else should follow close brace '}'\n" . $hereprev);
2842 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2843 $previndent == $indent) {
2844 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2846 # Find out what is on the end of the line after the
2848 substr($s, 0, length($c), '');
2851 if ($s =~ /^\s*;/) {
2852 ERROR("WHILE_AFTER_BRACE",
2853 "while should follow close brace '}'\n" . $hereprev);
2857 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2858 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2859 # print "No studly caps, use _\n";
2860 # print "$herecurr";
2864 #no spaces allowed after \ in define
2865 if ($line=~/\#\s*define.*\\\s$/) {
2866 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2867 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
2870 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2871 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2873 my $checkfile = "include/linux/$file";
2874 if (-f "$root/$checkfile" &&
2875 $realfile ne $checkfile &&
2876 $1 !~ /$allowed_asm_includes/)
2878 if ($realfile =~ m{^arch/}) {
2879 CHK("ARCH_INCLUDE_LINUX",
2880 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2882 WARN("INCLUDE_LINUX",
2883 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2888 # multi-statement macros should be enclosed in a do while loop, grab the
2889 # first statement and ensure its the whole macro if its not enclosed
2890 # in a known good container
2891 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2892 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2895 my ($off, $dstat, $dcond, $rest);
2897 ($dstat, $dcond, $ln, $cnt, $off) =
2898 ctx_statement_block($linenr, $realcnt, 0);
2900 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2901 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2903 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2905 $dstat =~ s/\\\n.//g;
2906 $dstat =~ s/^\s*//s;
2907 $dstat =~ s/\s*$//s;
2909 # Flatten any parentheses and braces
2910 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2911 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2912 $dstat =~ s/\[[^\[\]]*\]/1/)
2916 my $exceptions = qr{
2928 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2930 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
2931 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
2932 $dstat !~ /^(?:$Ident|-?$Constant)$/ && # 10 // foo()
2933 $dstat !~ /$exceptions/ &&
2934 $dstat !~ /^\.$Ident\s*=/ && # .foo =
2935 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
2936 $dstat !~ /^for\s*$Constant$/ && # for (...)
2937 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
2938 $dstat !~ /^do\s*{/ && # do {...
2939 $dstat !~ /^\({/) # ({...
2942 my $herectx = $here . "\n";
2943 my $cnt = statement_rawlines($ctx);
2945 for (my $n = 0; $n < $cnt; $n++) {
2946 $herectx .= raw_line($linenr, $n) . "\n";
2949 if ($dstat =~ /;/) {
2950 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2951 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
2953 ERROR("COMPLEX_MACRO",
2954 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
2959 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2960 # all assignments may have only one of the following with an assignment:
2963 # VMLINUX_SYMBOL(...)
2964 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2965 WARN("MISSING_VMLINUX_SYMBOL",
2966 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2969 # check for redundant bracing round if etc
2970 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2971 my ($level, $endln, @chunks) =
2972 ctx_statement_full($linenr, $realcnt, 1);
2973 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2974 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2975 if ($#chunks > 0 && $level == 0) {
2978 my $herectx = $here . "\n";
2979 my $ln = $linenr - 1;
2980 for my $chunk (@chunks) {
2981 my ($cond, $block) = @{$chunk};
2983 # If the condition carries leading newlines, then count those as offsets.
2984 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2985 my $offset = statement_rawlines($whitespace) - 1;
2987 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2989 # We have looked at and allowed this specific line.
2990 $suppress_ifbraces{$ln + $offset} = 1;
2992 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2993 $ln += statement_rawlines($block) - 1;
2995 substr($block, 0, length($cond), '');
2997 $seen++ if ($block =~ /^\s*{/);
2999 #print "cond<$cond> block<$block> allowed<$allowed>\n";
3000 if (statement_lines($cond) > 1) {
3001 #print "APW: ALLOWED: cond<$cond>\n";
3004 if ($block =~/\b(?:if|for|while)\b/) {
3005 #print "APW: ALLOWED: block<$block>\n";
3008 if (statement_block_size($block) > 1) {
3009 #print "APW: ALLOWED: lines block<$block>\n";
3013 if ($seen && !$allowed) {
3015 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3019 if (!defined $suppress_ifbraces{$linenr - 1} &&
3020 $line =~ /\b(if|while|for|else)\b/) {
3023 # Check the pre-context.
3024 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3025 #print "APW: ALLOWED: pre<$1>\n";
3029 my ($level, $endln, @chunks) =
3030 ctx_statement_full($linenr, $realcnt, $-[0]);
3032 # Check the condition.
3033 my ($cond, $block) = @{$chunks[0]};
3034 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3035 if (defined $cond) {
3036 substr($block, 0, length($cond), '');
3038 if (statement_lines($cond) > 1) {
3039 #print "APW: ALLOWED: cond<$cond>\n";
3042 if ($block =~/\b(?:if|for|while)\b/) {
3043 #print "APW: ALLOWED: block<$block>\n";
3046 if (statement_block_size($block) > 1) {
3047 #print "APW: ALLOWED: lines block<$block>\n";
3050 # Check the post-context.
3051 if (defined $chunks[1]) {
3052 my ($cond, $block) = @{$chunks[1]};
3053 if (defined $cond) {
3054 substr($block, 0, length($cond), '');
3056 if ($block =~ /^\s*\{/) {
3057 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3061 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3062 my $herectx = $here . "\n";
3063 my $cnt = statement_rawlines($block);
3065 for (my $n = 0; $n < $cnt; $n++) {
3066 $herectx .= raw_line($linenr, $n) . "\n";
3070 "braces {} are not necessary for single statement blocks\n" . $herectx);
3074 # don't include deprecated include files (uses RAW line)
3075 for my $inc (keys %dep_includes) {
3076 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
3077 ERROR("DEPRECATED_INCLUDE",
3078 "Don't use <$inc>, include " .
3079 "$dep_includes{$inc} instead\n" .
3084 # don't use deprecated functions
3085 for my $func (keys %dep_functions) {
3086 if ($line =~ /\b$func\b/) {
3087 ERROR("DEPRECATED_FUNCTION",
3088 "$func is deprecated, " .
3089 "use $dep_functions{$func} instead\n" .
3094 # try to replace assertions with error handling
3095 if ($line =~ /\bLASSERTF?\s*\(/) {
3097 "try to replace assertions with error handling\n" .
3101 # avoid new console messages
3102 if ($line =~ /\bLCONSOLE[A-Z_]*\s*\(/) {
3104 "avoid adding new console messages\n" .
3108 # minimize new CERROR messages
3109 if ($line =~ /\bC(EMERG|ERROR|NETERR|WARN)\s*\(/) {
3111 "think hard before adding new CERROR messages\n" .
3115 # no volatiles please
3116 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3117 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3119 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3123 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3124 CHK("REDUNDANT_CODE",
3125 "if this code is redundant consider removing it\n" .
3129 # check for needless kfree() checks
3130 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3132 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3133 WARN("NEEDLESS_KFREE",
3134 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
3137 # check for needless usb_free_urb() checks
3138 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3140 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
3141 WARN("NEEDLESS_USB_FREE_URB",
3142 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
3146 # prefer usleep_range over udelay
3147 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3148 # ignore udelay's < 10, however
3149 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3151 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3155 # warn about unexpectedly long msleep's
3156 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3159 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3163 # warn about #ifdefs in C files
3164 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3165 # print "#ifdef in C files should be avoided\n";
3166 # print "$herecurr";
3170 # warn about spacing in #ifdefs
3171 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3173 "exactly one space required after that #$1\n" . $herecurr);
3176 # check for spinlock_t definitions without a comment.
3177 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3178 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3180 if (!ctx_has_comment($first_line, $linenr)) {
3181 CHK("UNCOMMENTED_DEFINITION",
3182 "$1 definition without comment\n" . $herecurr);
3185 # check for memory barriers without a comment.
3186 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3187 if (!ctx_has_comment($first_line, $linenr)) {
3188 CHK("MEMORY_BARRIER",
3189 "memory barrier without comment\n" . $herecurr);
3192 # check of hardware specific defines
3193 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3195 "architecture specific defines should be avoided\n" . $herecurr);
3198 # Check that the storage class is at the beginning of a declaration
3199 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3200 WARN("STORAGE_CLASS",
3201 "storage class should be at the beginning of the declaration\n" . $herecurr)
3204 # check the location of the inline attribute, that it is between
3205 # storage class and type.
3206 if ($line =~ /\b$Type\s+$Inline\b/ ||
3207 $line =~ /\b$Inline\s+$Storage\b/) {
3208 ERROR("INLINE_LOCATION",
3209 "inline keyword should sit between storage class and type\n" . $herecurr);
3212 # Check for __inline__ and __inline, prefer inline
3213 if ($line =~ /\b(__inline__|__inline)\b/) {
3215 "plain inline is preferred over $1\n" . $herecurr);
3218 # Check for __attribute__ packed, prefer __packed
3219 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3220 WARN("PREFER_PACKED",
3221 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3224 # Check for __attribute__ aligned, prefer __aligned
3225 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3226 WARN("PREFER_ALIGNED",
3227 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3230 # Check for __attribute__ format(printf, prefer __printf
3231 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3232 WARN("PREFER_PRINTF",
3233 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3236 # check for sizeof(&)
3237 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3238 WARN("SIZEOF_ADDRESS",
3239 "sizeof(& should be avoided\n" . $herecurr);
3242 # check for line continuations in quoted strings with odd counts of "
3243 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3244 WARN("LINE_CONTINUATIONS",
3245 "Avoid line continuations in quoted strings\n" . $herecurr);
3248 # Check for misused memsets
3249 if (defined $stat &&
3250 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3256 if ($ms_size =~ /^(0x|)0$/i) {
3258 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3259 } elsif ($ms_size =~ /^(0x|)1$/i) {
3261 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3265 # typecasts on min/max could be min_t/max_t
3266 if (defined $stat &&
3267 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3268 if (defined $2 || defined $8) {
3270 my $cast1 = deparenthesize($2);
3272 my $cast2 = deparenthesize($8);
3276 if ($cast1 ne "" && $cast2 ne "") {
3277 $cast = "$cast1 or $cast2";
3278 } elsif ($cast1 ne "") {
3284 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3288 # check for new externs in .c files.
3289 if ($realfile =~ /\.c$/ && defined $stat &&
3290 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3292 my $function_name = $1;
3293 my $paren_space = $2;
3296 if (defined $cond) {
3297 substr($s, 0, length($cond), '');
3299 if ($s =~ /^\s*;/ &&
3300 $function_name ne 'uninitialized_var')
3302 WARN("AVOID_EXTERNS",
3303 "externs should be avoided in .c files\n" . $herecurr);
3306 if ($paren_space =~ /\n/) {
3307 WARN("FUNCTION_ARGUMENTS",
3308 "arguments for function declarations should follow identifier\n" . $herecurr);
3311 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3312 $stat =~ /^.\s*extern\s+/)
3314 WARN("AVOID_EXTERNS",
3315 "externs should be avoided in .c files\n" . $herecurr);
3318 # checks for new __setup's
3319 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3322 if (!grep(/$name/, @setup_docs)) {
3323 CHK("UNDOCUMENTED_SETUP",
3324 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3328 # check for pointless casting of kmalloc return
3329 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3330 WARN("UNNECESSARY_CASTS",
3331 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3334 # check for multiple semicolons
3335 if ($line =~ /;\s*;\s*$/) {
3336 WARN("ONE_SEMICOLON",
3337 "Statements terminations use 1 semicolon\n" . $herecurr);
3340 # check for gcc specific __FUNCTION__
3341 if ($line =~ /__FUNCTION__/) {
3343 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3346 # check for semaphores initialized locked
3347 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3348 WARN("CONSIDER_COMPLETION",
3349 "consider using a completion\n" . $herecurr);
3352 # recommend kstrto* over simple_strto* and strict_strto*
3353 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3354 WARN("CONSIDER_KSTRTO",
3355 "$1 is obsolete, use k$3 instead\n" . $herecurr);
3357 # check for __initcall(), use device_initcall() explicitly please
3358 if ($line =~ /^.\s*__initcall\s*\(/) {
3359 WARN("USE_DEVICE_INITCALL",
3360 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3362 # check for various ops structs, ensure they are const.
3363 my $struct_ops = qr{acpi_dock_ops|
3364 address_space_operations|
3366 block_device_operations|
3371 file_lock_operations|
3381 lock_manager_operations|
3387 pipe_buf_operations|
3388 platform_hibernation_ops|
3389 platform_suspend_ops|
3394 soc_pcmcia_socket_ops|
3400 if ($line !~ /\bconst\b/ &&
3401 $line =~ /\bstruct\s+($struct_ops)\b/) {
3402 WARN("CONST_STRUCT",
3403 "struct $1 should normally be const\n" .
3407 # use of NR_CPUS is usually wrong
3408 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3409 if ($line =~ /\bNR_CPUS\b/ &&
3410 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3411 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3412 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3413 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3414 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3417 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3420 # check for %L{u,d,i} in strings
3422 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3423 $string = substr($rawline, $-[1], $+[1] - $-[1]);
3424 $string =~ s/%%/__/g;
3425 if ($string =~ /(?<!%)%L[udi]/) {
3427 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3432 # whine mightly about in_atomic
3433 if ($line =~ /\bin_atomic\s*\(/) {
3434 if ($realfile =~ m@^drivers/@) {
3436 "do not use in_atomic in drivers\n" . $herecurr);
3437 } elsif ($realfile !~ m@^kernel/@) {
3439 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3443 # check for lockdep_set_novalidate_class
3444 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3445 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3446 if ($realfile !~ m@^kernel/lockdep@ &&
3447 $realfile !~ m@^include/linux/lockdep@ &&
3448 $realfile !~ m@^drivers/base/core@) {
3450 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3454 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3455 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3456 WARN("EXPORTED_WORLD_WRITABLE",
3457 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3461 # If we have no input at all, then there is nothing to report on
3462 # so just keep quiet.
3463 if ($#rawlines == -1) {
3467 # In mailback mode only produce a report in the negative, for
3468 # things that appear to be patches.
3469 if ($mailback && ($clean == 1 || !$is_patch)) {
3473 # This is not a patch, and we are are in 'no-patch' mode so
3475 if (!$chk_patch && !$is_patch) {
3480 ERROR("NOT_UNIFIED_DIFF",
3481 "Does not appear to be a unified-diff format patch\n");
3483 if ($is_patch && $chk_signoff && $signoff == 0) {
3484 ERROR("MISSING_SIGN_OFF",
3485 "Missing Signed-off-by: line(s)\n");
3488 print report_dump();
3489 if ($summary && !($clean == 1 && $quiet == 1)) {
3490 print "$filename " if ($summary_file);
3491 print "total: $cnt_error errors, $cnt_warn warnings, " .
3492 (($check)? "$cnt_chk checks, " : "") .
3493 "$cnt_lines lines checked\n";
3494 print "\n" if ($quiet == 0);
3498 # If there were whitespace errors which cleanpatch can fix
3499 # then suggest that.
3500 if ($rpt_cleaners) {
3501 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3502 print " scripts/cleanfile\n\n";
3507 if (keys %ignore_type) {
3508 print "NOTE: Ignored message types:";
3509 foreach my $ignore (sort keys %ignore_type) {
3513 print "\n" if ($quiet == 0);
3516 if ($clean == 1 && $quiet == 0) {
3517 print "$vname has no obvious style problems and is ready for submission.\n"
3519 if ($clean == 0 && $quiet == 0) {
3521 $vname has style problems, please review.
3523 If any of these errors are false positives, please report
3524 them to the maintainer, see CHECKPATCH in MAINTAINERS.