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
14 our $SrcFile = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$};
18 use Getopt
::Long
qw(:config no_auto_abbrev);
23 my $chk_patch = undef;
24 my $chk_branch = undef;
43 $P [OPTION
]... [FILE
]...
44 $P [OPTION
]... [GIT
-REV
-LIST
]
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
53 --branch treat args as GIT revision list
54 --emacs emacs compile window format
55 --terse one line per report
56 -f
, --file treat FILE as regular source file
57 --strict fail
if only warnings are found
58 --root
=PATH PATH to the kernel tree root
59 --no-summary suppress the per
-file summary
60 --mailback only produce a report
in case of warnings
/errors
61 --summary
-file include the filename
in summary
62 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
63 'values', 'possible', 'type', and 'attr' (default
65 --test
-only
=WORD report only warnings
/errors containing WORD
67 -h
, --help
, --version display this help
and exit
69 When FILE is
- read standard input
.
76 'q|quiet+' => \
$quiet,
78 'signoff!' => \
$chk_signoff,
79 'patch!' => \
$chk_patch,
80 'branch!' => \
$chk_branch,
84 'strict!' => \
$no_warnings,
86 'summary!' => \
$summary,
87 'mailback!' => \
$mailback,
88 'summary-file!' => \
$summary_file,
91 'test-only=s' => \
$tst_only,
101 print "$P: no input files\n";
105 if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
106 $chk_branch = $ARGV[0] =~ /.\.\./ ?
1 : 0;
107 $file = $ARGV[0] =~ /$SrcFile/ ?
1 : 0;
108 $chk_patch = $chk_branch || $file ?
0 : 1;
109 } elsif (!defined $chk_branch && !defined $chk_patch) {
111 $chk_branch = $chk_patch = 0;
113 $chk_branch = $ARGV[0] =~ /.\.\./ ?
1 : 0;
114 $chk_patch = $chk_branch ?
0 : 1;
116 } elsif (!defined $chk_branch && !defined $file) {
118 $chk_branch = $file = 0;
120 $chk_branch = $ARGV[0] =~ /.\.\./ ?
1 : 0;
121 $file = $chk_branch ?
0 : 1;
123 } elsif (!defined $chk_patch && !defined $file) {
125 $chk_patch = $file = 0;
127 $file = $ARGV[0] =~ /$SrcFile/ ?
1 : 0;
128 $chk_patch = $file ?
0 : 1;
130 } elsif (!defined $chk_branch) {
131 $chk_branch = $chk_patch || $file ?
0 : 1;
132 } elsif (!defined $chk_patch) {
133 $chk_patch = $chk_branch || $file ?
0 : 1;
134 } elsif (!defined $file) {
135 $file = $chk_patch || $chk_branch ?
0 : 1;
138 if (($chk_patch && $chk_branch) ||
139 ($chk_patch && $file) ||
140 ($chk_branch && $file)) {
141 die "Only one of --file, --branch, --patch is permitted\n";
143 if (!$chk_patch && !$chk_branch && !$file) {
144 die "One of --file, --branch, --patch is required\n";
148 my $dbg_possible = 0;
152 my $dbg_adv_checking = 0;
154 for my $key (keys %debug) {
156 eval "\${dbg_$key} = '$debug{$key}';";
160 my $rpt_cleaners = 0;
169 if (!top_of_kernel_tree
($root)) {
170 die "$P: $root: --root does not point at a valid tree\n";
173 if (top_of_kernel_tree
('.')) {
175 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
176 top_of_kernel_tree
($1)) {
181 if (!defined $root) {
182 print "Must be run from the top-level dir. of a kernel tree\n";
187 my $emitted_corrupt = 0;
190 [A
-Za
-z_
][A
-Za
-z\d_
]*
191 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
193 our $Storage = qr{extern|static|asmlinkage};
198 # Notes to $Attribute:
203 QEMU_WARN_UNUSED_RESULT
|
210 our $Inline = qr{inline};
211 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
212 our $Lval = qr{$Ident(?:$Member)*};
214 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
215 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
216 our $Compare = qr{<=|>=|==|!=|<|>};
220 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
227 our $NON_ASCII_UTF8 = qr{
228 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
229 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
230 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
231 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
232 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
233 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
234 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
238 [\x09\x0A\x0D\x20-\x7E] # ASCII
242 # There are still some false positives, but this catches most
244 our $typeTypedefs = qr{(?x
:
245 (?
![KMGTPE
]iB
) # IEC binary prefix (do not match)
246 [A
-Z
][A
-Z\d_
]*[a
-z
][A
-Za
-z\d_
]* # camelcase
247 | [A
-Z
][A
-Z\d_
]*AIOCB
# all uppercase
248 | [A
-Z
][A
-Z\d_
]*CPU
# all uppercase
249 | QEMUBH
# all uppercase
254 qr{(?:unsigned\s+)?char},
255 qr{(?:unsigned\s+)?short},
256 qr{(?:unsigned\s+)?int},
257 qr{(?:unsigned\s+)?long},
258 qr{(?:unsigned\s+)?long\s+int},
259 qr{(?:unsigned\s+)?long\s+long},
260 qr{(?:unsigned\s+)?long\s+long\s+int},
269 qr{${Ident}_handler
},
270 qr{${Ident}_handler_fn
},
271 qr{target_(?:u)?long},
305 # This can be modified by sub possible. Since it can be empty, be careful
306 # about regexes that always match, because they can cause infinite loops.
307 our @modifierList = (
311 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
312 if (@modifierList > 0) {
313 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
314 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
316 $Modifier = qr{(?:$Attribute|$Sparse)};
319 (?
:$Modifier\s
+|const\s
+)*
321 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)|
325 (?
:\s
+$Modifier|\s
+const
)*
329 (?
:[\s\
*]+\s
*const
|[\s\
*]+|(?
:\s
*\
[\s
*\
])+)?
330 (?
:\s
+$Inline|\s
+$Modifier)*
332 $Declare = qr{(?:$Storage\s+)?$Type};
336 $chk_signoff = 0 if ($file);
344 open($HASH, "-|", "git", "log", "--format=%H", $ARGV[0]) ||
345 die "$P: git log --format=%H $ARGV[0] failed - $!\n";
354 die "$P: no revisions returned for revlist '$chk_branch'\n"
357 for my $hash (@patches) {
359 open($FILE, '-|', "git", "show", $hash) ||
360 die "$P: git show $hash - $!\n";
367 if (!process
($hash)) {
374 for my $filename (@ARGV) {
377 open($FILE, '-|', "diff -u /dev/null $filename") ||
378 die "$P: $filename: diff failed - $!\n";
379 } elsif ($filename eq '-') {
380 open($FILE, '<&STDIN');
382 open($FILE, '<', "$filename") ||
383 die "$P: $filename: open failed - $!\n";
385 if ($filename eq '-') {
386 $vname = 'Your patch';
395 if (!process
($filename)) {
405 sub top_of_kernel_tree
{
409 "COPYING", "MAINTAINERS", "Makefile",
410 "README", "docs", "VERSION",
414 foreach my $check (@tree_check) {
415 if (! -e
$root . '/' . $check) {
427 for my $c (split(//, $str)) {
431 for (; ($n % 8) != 0; $n++) {
443 (my $res = shift) =~ tr/\t/ /c;
450 # Drop the diff line leader and expand tabs
452 $line = expand_tabs
($line);
454 # Pick the indent from the front of the line.
455 my ($white) = ($line =~ /^(\s*)/);
457 return (length($line), length($white));
460 my $sanitise_quote = '';
462 sub sanitise_line_reset
{
463 my ($in_comment) = @_;
466 $sanitise_quote = '*/';
468 $sanitise_quote = '';
481 # Always copy over the diff marker.
482 $res = substr($line, 0, 1);
484 for ($off = 1; $off < length($line); $off++) {
485 $c = substr($line, $off, 1);
487 # Comments we are wacking completely including the begin
488 # and end, all to $;.
489 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
490 $sanitise_quote = '*/';
492 substr($res, $off, 2, "$;$;");
496 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
497 $sanitise_quote = '';
498 substr($res, $off, 2, "$;$;");
502 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
503 $sanitise_quote = '//';
505 substr($res, $off, 2, $sanitise_quote);
510 # A \ in a string means ignore the next character.
511 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
513 substr($res, $off, 2, 'XX');
518 if ($c eq "'" || $c eq '"') {
519 if ($sanitise_quote eq '') {
520 $sanitise_quote = $c;
522 substr($res, $off, 1, $c);
524 } elsif ($sanitise_quote eq $c) {
525 $sanitise_quote = '';
529 #print "c<$c> SQ<$sanitise_quote>\n";
530 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
531 substr($res, $off, 1, $;);
532 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
533 substr($res, $off, 1, $;);
534 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
535 substr($res, $off, 1, 'X');
537 substr($res, $off, 1, $c);
541 if ($sanitise_quote eq '//') {
542 $sanitise_quote = '';
545 # The pathname on a #include may be surrounded by '<' and '>'.
546 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
547 my $clean = 'X' x
length($1);
548 $res =~ s@\
<.*\
>@
<$clean>@
;
550 # The whole of a #error is a string.
551 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
552 my $clean = 'X' x
length($1);
553 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
559 sub ctx_statement_block
{
560 my ($linenr, $remain, $off) = @_;
561 my $line = $linenr - 1;
578 @stack = (['', 0]) if ($#stack == -1);
580 #warn "CSB: blk<$blk> remain<$remain>\n";
581 # If we are about to drop off the end, pull in more
584 for (; $remain > 0; $line++) {
585 last if (!defined $lines[$line]);
586 next if ($lines[$line] =~ /^-/);
589 $blk .= $lines[$line] . "\n";
594 # Bail if there is no further context.
595 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
601 $c = substr($blk, $off, 1);
602 $remainder = substr($blk, $off);
604 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
606 # Handle nested #if/#else.
607 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
608 push(@stack, [ $type, $level ]);
609 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
610 ($type, $level) = @
{$stack[$#stack - 1]};
611 } elsif ($remainder =~ /^#\s*endif\b/) {
612 ($type, $level) = @
{pop(@stack)};
615 # Statement ends at the ';' or a close '}' at the
617 if ($level == 0 && $c eq ';') {
621 # An else is really a conditional as long as its not else if
622 if ($level == 0 && $coff_set == 0 &&
623 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
624 $remainder =~ /^(else)(?:\s|{)/ &&
625 $remainder !~ /^else\s+if\b/) {
626 $coff = $off + length($1) - 1;
628 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
629 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
632 if (($type eq '' || $type eq '(') && $c eq '(') {
636 if ($type eq '(' && $c eq ')') {
638 $type = ($level != 0)?
'(' : '';
640 if ($level == 0 && $coff < $soff) {
643 #warn "CSB: mark coff<$coff>\n";
646 if (($type eq '' || $type eq '{') && $c eq '{') {
650 if ($type eq '{' && $c eq '}') {
652 $type = ($level != 0)?
'{' : '';
655 if (substr($blk, $off + 1, 1) eq ';') {
663 # We are truly at the end, so shuffle to the next line.
670 my $statement = substr($blk, $soff, $off - $soff + 1);
671 my $condition = substr($blk, $soff, $coff - $soff + 1);
673 #warn "STATEMENT<$statement>\n";
674 #warn "CONDITION<$condition>\n";
676 #print "coff<$coff> soff<$off> loff<$loff>\n";
678 return ($statement, $condition,
679 $line, $remain + 1, $off - $loff + 1, $level);
682 sub statement_lines
{
685 # Strip the diff line prefixes and rip blank lines at start and end.
686 $stmt =~ s/(^|\n)./$1/g;
690 my @stmt_lines = ($stmt =~ /\n/g);
692 return $#stmt_lines + 2;
695 sub statement_rawlines
{
698 my @stmt_lines = ($stmt =~ /\n/g);
700 return $#stmt_lines + 2;
703 sub statement_block_size
{
706 $stmt =~ s/(^|\n)./$1/g;
712 my @stmt_lines = ($stmt =~ /\n/g);
713 my @stmt_statements = ($stmt =~ /;/g);
715 my $stmt_lines = $#stmt_lines + 2;
716 my $stmt_statements = $#stmt_statements + 1;
718 if ($stmt_lines > $stmt_statements) {
721 return $stmt_statements;
725 sub ctx_statement_full
{
726 my ($linenr, $remain, $off) = @_;
727 my ($statement, $condition, $level);
731 # Grab the first conditional/block pair.
732 ($statement, $condition, $linenr, $remain, $off, $level) =
733 ctx_statement_block
($linenr, $remain, $off);
734 #print "F: c<$condition> s<$statement> remain<$remain>\n";
735 push(@chunks, [ $condition, $statement ]);
736 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
737 return ($level, $linenr, @chunks);
740 # Pull in the following conditional/block pairs and see if they
741 # could continue the statement.
743 ($statement, $condition, $linenr, $remain, $off, $level) =
744 ctx_statement_block
($linenr, $remain, $off);
745 #print "C: c<$condition> s<$statement> remain<$remain>\n";
746 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
748 push(@chunks, [ $condition, $statement ]);
751 return ($level, $linenr, @chunks);
755 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
757 my $start = $linenr - 1;
764 my @stack = ($level);
765 for ($line = $start; $remain > 0; $line++) {
766 next if ($rawlines[$line] =~ /^-/);
769 $blk .= $rawlines[$line];
771 # Handle nested #if/#else.
772 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
773 push(@stack, $level);
774 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
775 $level = $stack[$#stack - 1];
776 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
777 $level = pop(@stack);
780 foreach my $c (split(//, $lines[$line])) {
781 ##print "C<$c>L<$level><$open$close>O<$off>\n";
787 if ($c eq $close && $level > 0) {
789 last if ($level == 0);
790 } elsif ($c eq $open) {
795 if (!$outer || $level <= 1) {
796 push(@res, $rawlines[$line]);
799 last if ($level == 0);
802 return ($level, @res);
804 sub ctx_block_outer
{
805 my ($linenr, $remain) = @_;
807 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
811 my ($linenr, $remain) = @_;
813 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
817 my ($linenr, $remain, $off) = @_;
819 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
822 sub ctx_block_level
{
823 my ($linenr, $remain) = @_;
825 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
827 sub ctx_statement_level
{
828 my ($linenr, $remain, $off) = @_;
830 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
833 sub ctx_locate_comment
{
834 my ($first_line, $end_line) = @_;
836 # Catch a comment on the end of the line itself.
837 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
838 return $current_comment if (defined $current_comment);
840 # Look through the context and try and figure out if there is a
843 $current_comment = '';
844 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
845 my $line = $rawlines[$linenr - 1];
847 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
850 if ($line =~ m@
/\
*@
) {
853 if (!$in_comment && $current_comment ne '') {
854 $current_comment = '';
856 $current_comment .= $line . "\n" if ($in_comment);
857 if ($line =~ m@\
*/@
) {
862 chomp($current_comment);
863 return($current_comment);
865 sub ctx_has_comment
{
866 my ($first_line, $end_line) = @_;
867 my $cmt = ctx_locate_comment
($first_line, $end_line);
869 ##print "LINE: $rawlines[$end_line - 1 ]\n";
870 ##print "CMMT: $cmt\n";
876 my ($linenr, $cnt) = @_;
878 my $offset = $linenr - 1;
883 $line = $rawlines[$offset++];
884 next if (defined($line) && $line =~ /^-/);
896 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
899 $coded = sprintf("^%c", unpack('C', $2) + 64);
908 my $av_preprocessor = 0;
914 $av_preprocessor = 0;
916 @av_paren_type = ('E');
917 $av_pend_colon = 'O';
920 sub annotate_values
{
921 my ($stream, $type) = @_;
924 my $var = '_' x
length($stream);
927 print "$stream\n" if ($dbg_values > 1);
929 while (length($cur)) {
930 @av_paren_type = ('E') if ($#av_paren_type < 0);
931 print " <" . join('', @av_paren_type) .
932 "> <$type> <$av_pending>" if ($dbg_values > 1);
933 if ($cur =~ /^(\s+)/o) {
934 print "WS($1)\n" if ($dbg_values > 1);
935 if ($1 =~ /\n/ && $av_preprocessor) {
936 $type = pop(@av_paren_type);
937 $av_preprocessor = 0;
940 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
941 print "CAST($1)\n" if ($dbg_values > 1);
942 push(@av_paren_type, $type);
945 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
946 print "DECLARE($1)\n" if ($dbg_values > 1);
949 } elsif ($cur =~ /^($Modifier)\s*/) {
950 print "MODIFIER($1)\n" if ($dbg_values > 1);
953 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
954 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
955 $av_preprocessor = 1;
956 push(@av_paren_type, $type);
962 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
963 print "UNDEF($1)\n" if ($dbg_values > 1);
964 $av_preprocessor = 1;
965 push(@av_paren_type, $type);
967 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
968 print "PRE_START($1)\n" if ($dbg_values > 1);
969 $av_preprocessor = 1;
971 push(@av_paren_type, $type);
972 push(@av_paren_type, $type);
975 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
976 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
977 $av_preprocessor = 1;
979 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
983 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
984 print "PRE_END($1)\n" if ($dbg_values > 1);
986 $av_preprocessor = 1;
988 # Assume all arms of the conditional end as this
989 # one does, and continue as if the #endif was not here.
991 push(@av_paren_type, $type);
994 } elsif ($cur =~ /^(\\\n)/o) {
995 print "PRECONT($1)\n" if ($dbg_values > 1);
997 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
998 print "ATTR($1)\n" if ($dbg_values > 1);
1002 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1003 print "SIZEOF($1)\n" if ($dbg_values > 1);
1009 } elsif ($cur =~ /^(if|while|for)\b/o) {
1010 print "COND($1)\n" if ($dbg_values > 1);
1014 } elsif ($cur =~/^(case)/o) {
1015 print "CASE($1)\n" if ($dbg_values > 1);
1016 $av_pend_colon = 'C';
1019 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1020 print "KEYWORD($1)\n" if ($dbg_values > 1);
1023 } elsif ($cur =~ /^(\()/o) {
1024 print "PAREN('$1')\n" if ($dbg_values > 1);
1025 push(@av_paren_type, $av_pending);
1029 } elsif ($cur =~ /^(\))/o) {
1030 my $new_type = pop(@av_paren_type);
1031 if ($new_type ne '_') {
1033 print "PAREN('$1') -> $type\n"
1034 if ($dbg_values > 1);
1036 print "PAREN('$1')\n" if ($dbg_values > 1);
1039 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1040 print "FUNC($1)\n" if ($dbg_values > 1);
1044 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1045 if (defined $2 && $type eq 'C' || $type eq 'T') {
1046 $av_pend_colon = 'B';
1047 } elsif ($type eq 'E') {
1048 $av_pend_colon = 'L';
1050 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1053 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1054 print "IDENT($1)\n" if ($dbg_values > 1);
1057 } elsif ($cur =~ /^($Assignment)/o) {
1058 print "ASSIGN($1)\n" if ($dbg_values > 1);
1061 } elsif ($cur =~/^(;|{|})/) {
1062 print "END($1)\n" if ($dbg_values > 1);
1064 $av_pend_colon = 'O';
1066 } elsif ($cur =~/^(,)/) {
1067 print "COMMA($1)\n" if ($dbg_values > 1);
1070 } elsif ($cur =~ /^(\?)/o) {
1071 print "QUESTION($1)\n" if ($dbg_values > 1);
1074 } elsif ($cur =~ /^(:)/o) {
1075 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1077 substr($var, length($res), 1, $av_pend_colon);
1078 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1083 $av_pend_colon = 'O';
1085 } elsif ($cur =~ /^(\[)/o) {
1086 print "CLOSE($1)\n" if ($dbg_values > 1);
1089 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1092 print "OPV($1)\n" if ($dbg_values > 1);
1099 substr($var, length($res), 1, $variant);
1102 } elsif ($cur =~ /^($Operators)/o) {
1103 print "OP($1)\n" if ($dbg_values > 1);
1104 if ($1 ne '++' && $1 ne '--') {
1108 } elsif ($cur =~ /(^.)/o) {
1109 print "C($1)\n" if ($dbg_values > 1);
1112 $cur = substr($cur, length($1));
1113 $res .= $type x
length($1);
1117 return ($res, $var);
1121 my ($possible, $line) = @_;
1122 my $notPermitted = qr{(?
:
1137 ^(?
:typedef
|struct
|enum
)\b|
1140 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1141 if ($possible !~ $notPermitted) {
1142 # Check for modifiers.
1143 $possible =~ s/\s*$Storage\s*//g;
1144 $possible =~ s/\s*$Sparse\s*//g;
1145 if ($possible =~ /^\s*$/) {
1147 } elsif ($possible =~ /\s/) {
1148 $possible =~ s/\s*(?:$Type|\#\#)\s*//g;
1149 for my $modifier (split(' ', $possible)) {
1150 if ($modifier !~ $notPermitted) {
1151 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1152 push(@modifierList, $modifier);
1157 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1158 push(@typeList, $possible);
1162 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1169 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1172 my $line = $prefix . $_[0];
1174 $line = (split('\n', $line))[0] . "\n" if ($terse);
1176 push(our @report, $line);
1184 if (report
("ERROR: $_[0]\n")) {
1190 if (report
("WARNING: $_[0]\n")) {
1197 my $filename = shift;
1203 my $stashrawline="";
1214 my $in_header_lines = $file ?
0 : 1;
1215 my $in_commit_log = 0; #Scanning lines before patch
1216 my $reported_maintainer_file = 0;
1217 my $non_utf8_charset = 0;
1225 # Trace the real file/line as we go.
1231 my $comment_edge = 0;
1235 my $prev_values = 'E';
1238 my %suppress_ifbraces;
1239 my %suppress_whiletrailers;
1240 my %suppress_export;
1242 # Pre-scan the patch sanitizing the lines.
1244 sanitise_line_reset
();
1246 foreach my $rawline (@rawlines) {
1250 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1259 # Guestimate if this is a continuing comment. Run
1260 # the context looking for a comment "edge". If this
1261 # edge is a close comment then we must be in a comment
1265 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1266 next if (defined $rawlines[$ln - 1] &&
1267 $rawlines[$ln - 1] =~ /^-/);
1269 #print "RAW<$rawlines[$ln - 1]>\n";
1270 last if (!defined $rawlines[$ln - 1]);
1271 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1272 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1277 if (defined $edge && $edge eq '*/') {
1281 # Guestimate if this is a continuing comment. If this
1282 # is the start of a diff block and this line starts
1283 # ' *' then it is very likely a comment.
1284 if (!defined $edge &&
1285 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1290 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1291 sanitise_line_reset
($in_comment);
1293 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1294 # Standardise the strings and chars within the input to
1295 # simplify matching -- only bother with positive lines.
1296 $line = sanitise_line
($rawline);
1298 push(@lines, $line);
1301 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1306 #print "==>$rawline\n";
1307 #print "-->$line\n";
1314 foreach my $line (@lines) {
1317 my $rawline = $rawlines[$linenr - 1];
1319 #extract the line range in the file after the patch is applied
1320 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1322 $first_line = $linenr + 1;
1332 %suppress_ifbraces = ();
1333 %suppress_whiletrailers = ();
1334 %suppress_export = ();
1337 # track the line number as we move through the hunk, note that
1338 # new versions of GNU diff omit the leading space on completely
1339 # blank context lines so we need to count that too.
1340 } elsif ($line =~ /^( |\+|$)/) {
1342 $realcnt-- if ($realcnt != 0);
1344 # Measure the line length and indent.
1345 ($length, $indent) = line_stats
($rawline);
1347 # Track the previous line.
1348 ($prevline, $stashline) = ($stashline, $line);
1349 ($previndent, $stashindent) = ($stashindent, $indent);
1350 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1352 #warn "line<$line>\n";
1354 } elsif ($realcnt == 1) {
1358 my $hunk_line = ($realcnt != 0);
1360 #make up the handle for any error we report on this line
1361 $prefix = "$filename:$realline: " if ($emacs && $file);
1362 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1364 $here = "#$linenr: " if (!$file);
1365 $here = "#$realline: " if ($file);
1367 # extract the filename as it passes
1368 if ($line =~ /^diff --git.*?(\S+)$/) {
1370 $realfile =~ s@
^([^/]*)/@@
if (!$file);
1371 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1373 $realfile =~ s@
^([^/]*)/@@
if (!$file);
1376 if (!$file && $tree && $p1_prefix ne '' &&
1377 -e
"$root/$p1_prefix") {
1378 WARN
("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1384 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1386 my $hereline = "$here\n$rawline\n";
1387 my $herecurr = "$here\n$rawline\n";
1388 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1390 $cnt_lines++ if ($realcnt != 0);
1392 # Check for incorrect file permissions
1393 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1394 my $permhere = $here . "FILE: $realfile\n";
1395 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1396 ERROR
("do not set execute permissions for source files\n" . $permhere);
1400 # Accept git diff extended headers as valid patches
1401 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\
.\
-]+\s
*$/) {
1405 #check the patch for a signoff:
1406 if ($line =~ /^\s*signed-off-by:/i) {
1407 # This is a signoff, if ugly, so do not double report.
1411 if (!($line =~ /^\s*Signed-off-by:/)) {
1412 ERROR
("The correct form is \"Signed-off-by\"\n" .
1415 if ($line =~ /^\s*signed-off-by:\S/i) {
1416 ERROR
("space required after Signed-off-by:\n" .
1421 # Check if MAINTAINERS is being updated. If so, there's probably no need to
1422 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
1423 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
1424 $reported_maintainer_file = 1;
1427 # Check for added, moved or deleted files
1428 if (!$reported_maintainer_file && !$in_commit_log &&
1429 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
1430 $line =~ /^rename (?:from|to) [\w\/\
.\
-]+\s
*$/ ||
1431 ($line =~ /\{\s*([\w\/\
.\
-]*)\s
*\
=\
>\s
*([\w\
/\.\-]*)\s*\}/ &&
1432 (defined($1) || defined($2))))) {
1433 $reported_maintainer_file = 1;
1434 WARN
("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
1437 # Check for wrappage within a valid hunk of the file
1438 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1439 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1440 $herecurr) if (!$emitted_corrupt++);
1443 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1444 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1445 $rawline !~ m/^$UTF8*$/) {
1446 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1448 my $blank = copy_spacing
($rawline);
1449 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1450 my $hereptr = "$hereline$ptr\n";
1452 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1455 # Check if it's the start of a commit log
1456 # (not a header line and we haven't seen the patch filename)
1457 if ($in_header_lines && $realfile =~ /^$/ &&
1458 !($rawline =~ /^\s+\S/ ||
1459 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
1460 $in_header_lines = 0;
1464 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1465 # declined it, i.e defined some charset where it is missing.
1466 if ($in_header_lines &&
1467 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1469 $non_utf8_charset = 1;
1472 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1473 $rawline =~ /$NON_ASCII_UTF8/) {
1474 WARN
("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1477 # ignore non-hunk lines and lines being removed
1478 next if (!$hunk_line || $line =~ /^-/);
1480 # ignore files that are being periodically imported from Linux
1481 next if ($realfile =~ /^(linux-headers|include\/standard
-headers
)\
//);
1483 #trailing whitespace
1484 if ($line =~ /^\+.*\015/) {
1485 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1486 ERROR
("DOS line endings\n" . $herevet);
1488 } elsif ($realfile =~ /^docs\/.+\
.txt
/ ||
1489 $realfile =~ /^docs\/.+\
.md
/) {
1490 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1491 # TODO: properly check we're in a code block
1492 # (surrounding text is 4-column aligned)
1493 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1494 ERROR
("code blocks in documentation should have " .
1495 "empty lines with exactly 4 columns of " .
1496 "whitespace\n" . $herevet);
1498 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1499 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1500 ERROR
("trailing whitespace\n" . $herevet);
1504 # checks for trace-events files
1505 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1506 if ($rawline =~ /%[-+ 0]*#/) {
1507 ERROR
("Don't use '#' flag of printf format ('%#') in " .
1508 "trace-events, use '0x' prefix instead\n" . $herecurr);
1511 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1513 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
1514 my $tmpline = $rawline;
1515 $tmpline =~ s/($hex[.:\/ ])+$hex//g
;
1517 if ($tmpline =~ /(?<!0x)$hex/) {
1518 ERROR
("Hex numbers must be prefixed with '0x'\n" .
1524 # check we are in a valid source file if not then ignore this hunk
1525 next if ($realfile !~ /$SrcFile/);
1527 #90 column limit; exempt URLs, if no other words on line
1528 if ($line =~ /^\+/ &&
1529 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1530 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1534 ERROR
("line over 90 characters\n" . $herecurr);
1536 WARN
("line over 80 characters\n" . $herecurr);
1540 # check for spaces before a quoted newline
1541 if ($rawline =~ /^.*\".*\s\\n/) {
1542 ERROR
("unnecessary whitespace before a quoted newline\n" . $herecurr);
1545 # check for adding lines without a newline.
1546 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1547 ERROR
("adding a line without newline at end of file\n" . $herecurr);
1550 # check for RCS/CVS revision markers
1551 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1552 ERROR
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1555 # tabs are only allowed in assembly source code, and in
1556 # some scripts we imported from other projects.
1557 next if ($realfile =~ /\.(s|S)$/);
1558 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1560 if ($rawline =~ /^\+.*\t/) {
1561 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1562 ERROR
("code indent should never use tabs\n" . $herevet);
1566 # check we are in a valid C source file if not then ignore this hunk
1567 next if ($realfile !~ /\.(h|c|cpp)$/);
1569 # Check for potential 'bare' types
1570 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1572 if ($realcnt && $line =~ /.\s*\S/) {
1573 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1574 ctx_statement_block
($linenr, $realcnt, 0);
1575 $stat =~ s/\n./\n /g;
1576 $cond =~ s/\n./\n /g;
1578 # Find the real next line.
1579 $realline_next = $line_nr_next;
1580 if (defined $realline_next &&
1581 (!defined $lines[$realline_next - 1] ||
1582 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1589 # Ignore goto labels.
1590 if ($s =~ /$Ident:\*$/s) {
1592 # Ignore functions being called
1593 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1595 } elsif ($s =~ /^.\s*else\b/s) {
1597 # declarations always start with types
1598 } 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) {
1601 possible
($type, "A:" . $s);
1603 # definitions in global scope can only start with types
1604 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1605 possible
($1, "B:" . $s);
1608 # any (foo ... *) is a pointer cast, and foo is a type
1609 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1610 possible
($1, "C:" . $s);
1613 # Check for any sort of function declaration.
1614 # int foo(something bar, other baz);
1615 # void (*store_gdt)(x86_descr_ptr *);
1616 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1617 my ($name_len) = length($1);
1620 substr($ctx, 0, $name_len + 1, '');
1621 $ctx =~ s/\)[^\)]*$//;
1623 for my $arg (split(/\s*,\s*/, $ctx)) {
1624 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1626 possible
($1, "D:" . $s);
1634 # Checks which may be anchored in the context.
1637 # Check for switch () and associated case and default
1638 # statements should be at the same indent.
1639 if ($line=~/\bswitch\s*\(.*\)/) {
1642 my @ctx = ctx_block_outer
($linenr, $realcnt);
1644 for my $ctx (@ctx) {
1645 my ($clen, $cindent) = line_stats
($ctx);
1646 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1647 $indent != $cindent) {
1648 $err .= "$sep$ctx\n";
1655 ERROR
("switch and case should be at the same indent\n$hereline$err");
1659 # if/while/etc brace do not go on next line, unless defining a do while loop,
1660 # or if that brace on the next line is for something else
1661 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1662 my $pre_ctx = "$1$2";
1664 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1665 my $ctx_cnt = $realcnt - $#ctx - 1;
1666 my $ctx = join("\n", @ctx);
1668 my $ctx_ln = $linenr;
1669 my $ctx_skip = $realcnt;
1671 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1672 defined $lines[$ctx_ln - 1] &&
1673 $lines[$ctx_ln - 1] =~ /^-/)) {
1674 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1675 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1679 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1680 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1682 # The length of the "previous line" is checked against 80 because it
1683 # includes the + at the beginning of the line (if the actual line has
1684 # 79 or 80 characters, it is no longer possible to add a space and an
1685 # opening brace there)
1686 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1687 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1688 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1689 ERROR
("that open brace { should be on the previous line\n" .
1690 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1692 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1693 $ctx =~ /\)\s*\;\s*$/ &&
1694 defined $lines[$ctx_ln - 1])
1696 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1697 if ($nindent > $indent) {
1698 ERROR
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1699 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1704 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1705 if ($line =~ /while\s*\((0|false)\);/) {
1706 ERROR
("suspicious ; after while (0)\n" . $herecurr);
1709 # Check relative indent for conditionals and blocks.
1710 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1711 my ($s, $c) = ($stat, $cond);
1713 substr($s, 0, length($c), '');
1715 # Make sure we remove the line prefixes as we have
1716 # none on the first line, and are going to readd them
1720 # Find out how long the conditional actually is.
1721 my @newlines = ($c =~ /\n/gs);
1722 my $cond_lines = 1 + $#newlines;
1724 # We want to check the first line inside the block
1725 # starting at the end of the conditional, so remove:
1726 # 1) any blank line termination
1727 # 2) any opening brace { on end of the line
1729 my $continuation = 0;
1731 $s =~ s/^.*\bdo\b//;
1733 if ($s =~ s/^\s*\\//) {
1736 if ($s =~ s/^\s*?\n//) {
1741 # Also ignore a loop construct at the end of a
1742 # preprocessor statement.
1743 if (($prevline =~ /^.\s*#\s*define\s/ ||
1744 $prevline =~ /\\\s*$/) && $continuation == 0) {
1750 while ($cond_ptr != $cond_lines) {
1751 $cond_ptr = $cond_lines;
1753 # If we see an #else/#elif then the code
1755 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1760 # 1) blank lines, they should be at 0,
1761 # 2) preprocessor lines, and
1763 if ($continuation ||
1765 $s =~ /^\s*#\s*?/ ||
1766 $s =~ /^\s*$Ident\s*:/) {
1767 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
1768 if ($s =~ s/^.*?\n//) {
1774 my (undef, $sindent) = line_stats
("+" . $s);
1775 my $stat_real = raw_line
($linenr, $cond_lines);
1777 # Check if either of these lines are modified, else
1778 # this is not this patch's fault.
1779 if (!defined($stat_real) ||
1780 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1783 if (defined($stat_real) && $cond_lines > 1) {
1784 $stat_real = "[...]\n$stat_real";
1787 #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";
1789 if ($check && (($sindent % 4) != 0 ||
1790 ($sindent <= $indent && $s ne ''))) {
1791 ERROR
("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1795 # Track the 'values' across context and added lines.
1796 my $opline = $line; $opline =~ s/^./ /;
1797 my ($curr_values, $curr_vars) =
1798 annotate_values
($opline . "\n", $prev_values);
1799 $curr_values = $prev_values . $curr_values;
1801 my $outline = $opline; $outline =~ s/\t/ /g;
1802 print "$linenr > .$outline\n";
1803 print "$linenr > $curr_values\n";
1804 print "$linenr > $curr_vars\n";
1806 $prev_values = substr($curr_values, -1);
1808 #ignore lines not being added
1809 if ($line=~/^[^\+]/) {next;}
1811 # TEST: allow direct testing of the type matcher.
1813 if ($line =~ /^.\s*$Declare\s*$/) {
1814 ERROR
("TEST: is type\n" . $herecurr);
1815 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1816 ERROR
("TEST: is not type ($1 is)\n". $herecurr);
1820 # TEST: allow direct testing of the attribute matcher.
1822 if ($line =~ /^.\s*$Modifier\s*$/) {
1823 ERROR
("TEST: is attr\n" . $herecurr);
1824 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1825 ERROR
("TEST: is not attr ($1 is)\n". $herecurr);
1830 # check for initialisation to aggregates open brace on the next line
1831 if ($line =~ /^.\s*\{/ &&
1832 $prevline =~ /(?:^|[^=])=\s*$/) {
1833 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1837 # Checks which are anchored on the added line.
1840 # check for malformed paths in #include statements (uses RAW line)
1841 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1843 if ($path =~ m{//}) {
1844 ERROR
("malformed #include filename\n" .
1849 # no C99 // comments
1850 if ($line =~ m{//}) {
1851 ERROR
("do not use C99 // comments\n" . $herecurr);
1853 # Remove C99 comments.
1855 $opline =~ s@
//.*@@
;
1857 # check for global initialisers.
1858 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1859 ERROR
("do not initialise globals to 0 or NULL\n" .
1862 # check for static initialisers.
1863 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1864 ERROR
("do not initialise statics to 0 or NULL\n" .
1868 # * goes on variable not on type
1870 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1871 my ($from, $to) = ($1, $1);
1873 # Should start with a space.
1874 $to =~ s/^(\S)/ $1/;
1875 # Should not end with a space.
1877 # '*'s should not have spaces between.
1878 while ($to =~ s/\*\s+\*/\*\*/) {
1881 #print "from<$from> to<$to>\n";
1883 ERROR
("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1885 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1886 my ($from, $to, $ident) = ($1, $1, $2);
1888 # Should start with a space.
1889 $to =~ s/^(\S)/ $1/;
1890 # Should not end with a space.
1892 # '*'s should not have spaces between.
1893 while ($to =~ s/\*\s+\*/\*\*/) {
1895 # Modifiers should have spaces.
1896 $to =~ s/(\b$Modifier$)/$1 /;
1898 #print "from<$from> to<$to> ident<$ident>\n";
1899 if ($from ne $to && $ident !~ /^$Modifier$/) {
1900 ERROR
("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1904 # function brace can't be on same line, except for #defines of do while,
1905 # or if closed on same line
1906 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1907 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1908 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1911 # open braces for enum, union and struct go on the same line.
1912 if ($line =~ /^.\s*\{/ &&
1913 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1914 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1917 # missing space after union, struct or enum definition
1918 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1919 ERROR
("missing space after $1 definition\n" . $herecurr);
1922 # check for spacing round square brackets; allowed:
1923 # 1. with a type on the left -- int [] a;
1924 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1925 # 3. inside a curly brace -- = { [0...10] = 5 }
1926 # 4. after a comma -- [1] = 5, [2] = 6
1927 # 5. in a macro definition -- #define abc(x) [x] = y
1928 while ($line =~ /(.*?\s)\[/g) {
1929 my ($where, $prefix) = ($-[1], $1);
1930 if ($prefix !~ /$Type\s+$/ &&
1931 ($where != 0 || $prefix !~ /^.\s+$/) &&
1932 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1933 $prefix !~ /[,{:]\s+$/) {
1934 ERROR
("space prohibited before open square bracket '['\n" . $herecurr);
1938 # check for spaces between functions and their parentheses.
1939 while ($line =~ /($Ident)\s+\(/g) {
1941 my $ctx_before = substr($line, 0, $-[1]);
1942 my $ctx = "$ctx_before$name";
1944 # Ignore those directives where spaces _are_ permitted.
1946 if|for|while|switch
|return|case
|
1947 volatile
|__volatile__
|coroutine_fn
|
1948 __attribute__
|format
|__extension__
|
1952 # Ignore 'catch (...)' in C++
1953 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1955 # cpp #define statements have non-optional spaces, ie
1956 # if there is a space between the name and the open
1957 # parenthesis it is simply not a parameter group.
1958 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1960 # cpp #elif statement condition may start with a (
1961 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1963 # If this whole things ends with a type its most
1964 # likely a typedef for a function.
1965 } elsif ($ctx =~ /$Type$/) {
1968 ERROR
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1971 # Check operator spacing.
1972 if (!($line=~/\#\s*include/)) {
1974 <<=|>>=|<=|>=|==|!=|
1975 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1976 =>|->|<<|>>|<|>|=|!|~|
1977 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
1980 my @elements = split(/($ops|;)/, $opline);
1983 my $blank = copy_spacing
($opline);
1985 for (my $n = 0; $n < $#elements; $n += 2) {
1986 $off += length($elements[$n]);
1988 # Pick up the preceding and succeeding characters.
1989 my $ca = substr($opline, 0, $off);
1991 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1992 $cc = substr($opline, $off + length($elements[$n + 1]));
1994 my $cb = "$ca$;$cc";
1997 $a = 'V' if ($elements[$n] ne '');
1998 $a = 'W' if ($elements[$n] =~ /\s$/);
1999 $a = 'C' if ($elements[$n] =~ /$;$/);
2000 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2001 $a = 'O' if ($elements[$n] eq '');
2002 $a = 'E' if ($ca =~ /^\s*$/);
2004 my $op = $elements[$n + 1];
2007 if (defined $elements[$n + 2]) {
2008 $c = 'V' if ($elements[$n + 2] ne '');
2009 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2010 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2011 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2012 $c = 'O' if ($elements[$n + 2] eq '');
2013 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2018 my $ctx = "${a}x${c}";
2020 my $at = "(ctx:$ctx)";
2022 my $ptr = substr($blank, 0, $off) . "^";
2023 my $hereptr = "$hereline$ptr\n";
2025 # Pull out the value of this operator.
2026 my $op_type = substr($curr_values, $off + 1, 1);
2028 # Get the full operator variant.
2029 my $opv = $op . substr($curr_vars, $off, 1);
2031 # Ignore operators passed as parameters.
2032 if ($op_type ne 'V' &&
2033 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2036 # } elsif ($op =~ /^$;+$/) {
2038 # ; should have either the end of line or a space or \ after it
2039 } elsif ($op eq ';') {
2040 if ($ctx !~ /.x[WEBC]/ &&
2041 $cc !~ /^\\/ && $cc !~ /^;/) {
2042 ERROR
("space required after that '$op' $at\n" . $hereptr);
2046 } elsif ($op eq '//') {
2048 # Ignore : used in class declaration in C++
2049 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2050 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2054 # : when part of a bitfield
2055 } elsif ($op eq '->' || $opv eq ':B') {
2056 if ($ctx =~ /Wx.|.xW/) {
2057 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
2060 # , must have a space on the right.
2061 # not required when having a single },{ on one line
2062 } elsif ($op eq ',') {
2063 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2064 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
2065 ERROR
("space required after that '$op' $at\n" . $hereptr);
2068 # '*' as part of a type definition -- reported already.
2069 } elsif ($opv eq '*_') {
2070 #warn "'*' is part of type\n";
2072 # unary operators should have a space before and
2073 # none after. May be left adjacent to another
2074 # unary operator, or a cast
2075 } elsif ($op eq '!' || $op eq '~' ||
2076 $opv eq '*U' || $opv eq '-U' ||
2077 $opv eq '&U' || $opv eq '&&U') {
2078 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2079 # '~' used as a name of Destructor
2081 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2082 ERROR
("space required before that '$op' $at\n" . $hereptr);
2084 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2085 # A unary '*' may be const
2087 } elsif ($ctx =~ /.xW/) {
2088 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2091 # unary ++ and unary -- are allowed no space on one side.
2092 } elsif ($op eq '++' or $op eq '--') {
2093 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2094 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
2096 if ($ctx =~ /Wx[BE]/ ||
2097 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2098 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2100 if ($ctx =~ /ExW/) {
2101 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2104 # A colon needs no spaces before when it is
2105 # terminating a case value or a label.
2106 } elsif ($opv eq ':C' || $opv eq ':L') {
2107 if ($ctx =~ /Wx./) {
2108 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2111 # All the others need spaces both sides.
2112 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2115 if ($realfile =~ /\.cpp|\.h$/) {
2116 # Ignore template arguments <...> in C++
2117 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2127 # Ignore email addresses <foo@bar>
2129 $cc =~ /^\S+\@\S+>/) ||
2131 $ca =~ /<\S+\@\S+$/))
2137 if (($opv eq ':O' && $ca =~ /\?$/) ||
2138 ($op eq '?' && $cc =~ /^:/)) {
2143 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
2146 $off += length($elements[$n + 1]);
2150 #need space before brace following if, while, etc
2151 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2153 ERROR
("space required before the open brace '{'\n" . $herecurr);
2156 # closing brace should have a space following it when it has anything
2158 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2159 ERROR
("space required after that close brace '}'\n" . $herecurr);
2162 # check spacing on square brackets
2163 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2164 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
2166 if ($line =~ /\s\]/) {
2167 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
2170 # check spacing on parentheses
2171 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2172 $line !~ /for\s*\(\s+;/) {
2173 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
2175 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2176 $line !~ /for\s*\(.*;\s+\)/ &&
2177 $line !~ /:\s+\)/) {
2178 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
2181 # Return is not a function.
2182 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2186 # Flatten any parentheses
2187 $value =~ s/\(/ \(/g;
2188 $value =~ s/\)/\) /g;
2189 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2190 $value !~ /(?
:$Ident|-?
$Constant)\s
*
2192 (?
:$Ident|-?
$Constant)/x
&&
2193 $value =~ s/\([^\(\)]*\)/1/) {
2195 #print "value<$value>\n";
2196 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2197 ERROR
("return is not a function, parentheses are not required\n" . $herecurr);
2199 } elsif ($spacing !~ /\s+/) {
2200 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2203 # Return of what appears to be an errno should normally be -'ve
2204 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2206 if ($name ne 'EOF' && $name ne 'ERROR') {
2207 ERROR
("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2211 # Need a space before open parenthesis after if, while etc
2212 if ($line=~/\b(if|while|for|switch)\(/) {
2213 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2216 # Check for illegal assignment in if conditional -- and check for trailing
2217 # statements after the conditional.
2218 if ($line =~ /do\s*(?!{)/) {
2219 my ($stat_next) = ctx_statement_block
($line_nr_next,
2220 $remain_next, $off_next);
2221 $stat_next =~ s/\n./\n /g;
2222 ##print "stat<$stat> stat_next<$stat_next>\n";
2224 if ($stat_next =~ /^\s*while\b/) {
2225 # If the statement carries leading newlines,
2226 # then count those as offsets.
2228 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2230 statement_rawlines
($whitespace) - 1;
2232 $suppress_whiletrailers{$line_nr_next +
2236 if (!defined $suppress_whiletrailers{$linenr} &&
2237 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2238 my ($s, $c) = ($stat, $cond);
2240 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2241 ERROR
("do not use assignment in if condition\n" . $herecurr);
2244 # Find out what is on the end of the line after the
2246 substr($s, 0, length($c), '');
2248 $s =~ s/$;//g; # Remove any comments
2249 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2250 $c !~ /}\s*while\s*/)
2252 # Find out how long the conditional actually is.
2253 my @newlines = ($c =~ /\n/gs);
2254 my $cond_lines = 1 + $#newlines;
2257 $stat_real = raw_line
($linenr, $cond_lines)
2258 . "\n" if ($cond_lines);
2259 if (defined($stat_real) && $cond_lines > 1) {
2260 $stat_real = "[...]\n$stat_real";
2263 ERROR
("trailing statements should be on next line\n" . $herecurr . $stat_real);
2267 # Check for bitwise tests written as boolean
2279 ERROR
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2282 # if and else should not have general statements after it
2283 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2285 $s =~ s/$;//g; # Remove any comments
2286 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2287 ERROR
("trailing statements should be on next line\n" . $herecurr);
2290 # if should not continue a brace
2291 if ($line =~ /}\s*if\b/) {
2292 ERROR
("trailing statements should be on next line\n" .
2295 # case and default should not have general statements after them
2296 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2298 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2302 ERROR
("trailing statements should be on next line\n" . $herecurr);
2305 # Check for }<nl>else {, these must be at the same
2306 # indent level to be relevant to each other.
2307 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2308 $previndent == $indent) {
2309 ERROR
("else should follow close brace '}'\n" . $hereprev);
2312 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2313 $previndent == $indent) {
2314 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2316 # Find out what is on the end of the line after the
2318 substr($s, 0, length($c), '');
2321 if ($s =~ /^\s*;/) {
2322 ERROR
("while should follow close brace '}'\n" . $hereprev);
2326 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2327 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2328 # print "No studly caps, use _\n";
2329 # print "$herecurr";
2333 #no spaces allowed after \ in define
2334 if ($line=~/\#\s*define.*\\\s$/) {
2335 ERROR
("Whitespace after \\ makes next lines useless\n" . $herecurr);
2338 # multi-statement macros should be enclosed in a do while loop, grab the
2339 # first statement and ensure its the whole macro if its not enclosed
2340 # in a known good container
2341 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2342 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2345 my ($off, $dstat, $dcond, $rest);
2348 my $args = defined($1);
2350 # Find the end of the macro and limit our statement
2352 while ($cnt > 0 && defined $lines[$ln - 1] &&
2353 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2355 $ctx .= $rawlines[$ln - 1] . "\n";
2356 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2359 $ctx .= $rawlines[$ln - 1];
2361 ($dstat, $dcond, $ln, $cnt, $off) =
2362 ctx_statement_block
($linenr, $ln - $linenr + 1, 0);
2363 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2364 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2366 # Extract the remainder of the define (if any) and
2367 # rip off surrounding spaces, and trailing \'s.
2369 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2370 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2371 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2372 $rest .= substr($lines[$ln - 1], $off) . "\n";
2378 $rest =~ s/\\\n.//g;
2382 # Clean up the original statement.
2384 substr($dstat, 0, length($dcond), '');
2386 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2389 $dstat =~ s/\\\n.//g;
2390 $dstat =~ s/^\s*//s;
2391 $dstat =~ s/\s*$//s;
2393 # Flatten any parentheses and braces
2394 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2395 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2396 $dstat =~ s/\[[^\{\}]*\]/1/)
2400 my $exceptions = qr{
2412 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2413 if ($rest ne '' && $rest ne ',') {
2414 if ($rest !~ /while\s*\(/ &&
2415 $dstat !~ /$exceptions/)
2417 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2420 } elsif ($ctx !~ /;/) {
2422 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2423 $dstat !~ /$exceptions/ &&
2424 $dstat !~ /^\.$Ident\s*=/ &&
2425 $dstat =~ /$Operators/)
2427 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2432 # check for missing bracing around if etc
2433 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2434 $line !~ /\#\s*if/) {
2437 # Check the pre-context.
2438 if ($line =~ /(\}.*?)$/) {
2441 if ($line !~ /else/) {
2442 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2447 my ($level, $endln, @chunks) =
2448 ctx_statement_full
($linenr, $realcnt, 1);
2450 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2451 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2454 if ($#chunks >= 0 && $level == 0) {
2456 my $herectx = $here . "\n";
2457 my $ln = $linenr - 1;
2458 for my $chunk (@chunks) {
2459 my ($cond, $block) = @
{$chunk};
2461 # If the condition carries leading newlines, then count those as offsets.
2462 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2463 my $offset = statement_rawlines
($whitespace) - 1;
2465 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2467 # We have looked at and allowed this specific line.
2468 $suppress_ifbraces{$ln + $offset} = 1;
2470 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2471 $ln += statement_rawlines
($block) - 1;
2473 substr($block, 0, length($cond), '');
2475 my $spaced_block = $block;
2476 $spaced_block =~ s/\n\+/ /g;
2478 $seen++ if ($spaced_block =~ /^\s*\{/);
2480 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2482 if (statement_lines
($cond) > 1) {
2483 print "APW: ALLOWED: cond<$cond>\n"
2487 if ($block =~/\b(?:if|for|while)\b/) {
2488 print "APW: ALLOWED: block<$block>\n"
2492 if (statement_block_size
($block) > 1) {
2493 print "APW: ALLOWED: lines block<$block>\n"
2498 if ($seen != ($#chunks + 1) && !$allowed) {
2499 ERROR
("braces {} are necessary for all arms of this statement\n" . $herectx);
2503 if (!defined $suppress_ifbraces{$linenr - 1} &&
2504 $line =~ /\b(if|while|for|else)\b/ &&
2505 $line !~ /\#\s*if/ &&
2506 $line !~ /\#\s*else/) {
2509 # Check the pre-context.
2510 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2513 if ($line !~ /else/) {
2514 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2520 my ($level, $endln, @chunks) =
2521 ctx_statement_full
($linenr, $realcnt, $-[0]);
2523 # Check the condition.
2524 my ($cond, $block) = @
{$chunks[0]};
2525 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2526 if $dbg_adv_checking;
2527 if (defined $cond) {
2528 substr($block, 0, length($cond), '');
2530 if (statement_lines
($cond) > 1) {
2531 print "APW: ALLOWED: cond<$cond>\n"
2535 if ($block =~/\b(?:if|for|while)\b/) {
2536 print "APW: ALLOWED: block<$block>\n"
2540 if (statement_block_size
($block) > 1) {
2541 print "APW: ALLOWED: lines block<$block>\n"
2545 # Check the post-context.
2546 if (defined $chunks[1]) {
2547 my ($cond, $block) = @
{$chunks[1]};
2548 if (defined $cond) {
2549 substr($block, 0, length($cond), '');
2551 if ($block =~ /^\s*\{/) {
2552 print "APW: ALLOWED: chunk-1 block<$block>\n"
2557 print "DCS: level=$level block<$block> allowed=$allowed\n"
2559 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2560 my $herectx = $here . "\n";;
2561 my $cnt = statement_rawlines
($block);
2563 for (my $n = 0; $n < $cnt; $n++) {
2564 $herectx .= raw_line
($linenr, $n) . "\n";;
2567 ERROR
("braces {} are necessary even for single statement blocks\n" . $herectx);
2571 # no volatiles please
2572 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2573 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ &&
2574 $line !~ /sig_atomic_t/ &&
2575 !ctx_has_comment
($first_line, $linenr)) {
2576 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr;
2581 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2582 ERROR
("if this code is redundant consider removing it\n" .
2586 # check for needless g_free() checks
2587 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2589 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2590 ERROR
("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2594 # warn about #ifdefs in C files
2595 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2596 # print "#ifdef in C files should be avoided\n";
2597 # print "$herecurr";
2601 # warn about spacing in #ifdefs
2602 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2603 ERROR
("exactly one space required after that #$1\n" . $herecurr);
2605 # check for memory barriers without a comment.
2606 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2607 if (!ctx_has_comment
($first_line, $linenr)) {
2608 ERROR
("memory barrier without comment\n" . $herecurr);
2611 # check of hardware specific defines
2612 # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2613 # where they might be necessary.
2614 if ($line =~ m@
^.\s
*\#\s
*if.*\b__@
) {
2615 WARN
("architecture specific defines should be avoided\n" . $herecurr);
2618 # Check that the storage class is at the beginning of a declaration
2619 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2620 ERROR
("storage class should be at the beginning of the declaration\n" . $herecurr)
2623 # check the location of the inline attribute, that it is between
2624 # storage class and type.
2625 if ($line =~ /\b$Type\s+$Inline\b/ ||
2626 $line =~ /\b$Inline\s+$Storage\b/) {
2627 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
2630 # check for sizeof(&)
2631 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2632 ERROR
("sizeof(& should be avoided\n" . $herecurr);
2635 # check for new externs in .c files.
2636 if ($realfile =~ /\.c$/ && defined $stat &&
2637 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2639 my $function_name = $1;
2640 my $paren_space = $2;
2643 if (defined $cond) {
2644 substr($s, 0, length($cond), '');
2646 if ($s =~ /^\s*;/ &&
2647 $function_name ne 'uninitialized_var')
2649 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2652 if ($paren_space =~ /\n/) {
2653 ERROR
("arguments for function declarations should follow identifier\n" . $herecurr);
2656 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2657 $stat =~ /^.\s*extern\s+/)
2659 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2662 # check for pointless casting of g_malloc return
2663 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2665 ERROR
("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2667 ERROR
("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2671 # check for gcc specific __FUNCTION__
2672 if ($line =~ /__FUNCTION__/) {
2673 ERROR
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2676 # recommend g_path_get_* over g_strdup(basename/dirname(...))
2677 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) {
2678 WARN
("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
2681 # recommend qemu_strto* over strto* for numeric conversions
2682 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2683 ERROR
("consider using qemu_$1 in preference to $1\n" . $herecurr);
2685 # recommend sigaction over signal for portability, when establishing a handler
2686 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2687 ERROR
("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2689 # check for module_init(), use category-specific init macros explicitly please
2690 if ($line =~ /^module_init\s*\(/) {
2691 ERROR
("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2693 # check for various ops structs, ensure they are const.
2694 my $struct_ops = qr{AIOCBInfo
|
2698 DisplayChangeListenerOps
|
2702 MemoryRegionIOMMUOps
|
2708 Spice
[A
-Z
][a
-zA
-Z0
-9]*Interface
|
2709 USBDesc
[A
-Z
][a
-zA
-Z0
-9]*|
2713 if ($line !~ /\bconst\b/ &&
2714 $line =~ /\b($struct_ops)\b.*=/) {
2715 ERROR
("initializer for struct $1 should normally be const\n" .
2719 # check for %L{u,d,i} in strings
2721 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2722 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2723 $string =~ s/%%/__/g;
2724 if ($string =~ /(?<!%)%L[udi]/) {
2725 ERROR
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2730 # QEMU specific tests
2731 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2732 ERROR
("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2735 # Qemu error function tests
2737 # Find newlines in error messages
2738 my $qemu_error_funcs = qr{error_setg
|
2741 error_setg_file_open
|
2753 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2754 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2757 # Continue checking for error messages that contains newlines. This
2758 # check handles cases where string literals are spread over multiple lines.
2760 # error_report("Error msg line #1"
2761 # "Error msg line #2\n");
2762 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2763 my $continued_str_literal = qr{\+\s*\".*\"};
2765 if ($rawline =~ /$quoted_newline_regex/) {
2766 # Backtrack to first line that does not contain only a quoted literal
2767 # and assume that it is the start of the statement.
2768 my $i = $linenr - 2;
2770 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2774 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2775 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2779 # check for non-portable libc calls that have portable alternatives in QEMU
2780 if ($line =~ /\bffs\(/) {
2781 ERROR
("use ctz32() instead of ffs()\n" . $herecurr);
2783 if ($line =~ /\bffsl\(/) {
2784 ERROR
("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2786 if ($line =~ /\bffsll\(/) {
2787 ERROR
("use ctz64() instead of ffsll()\n" . $herecurr);
2789 if ($line =~ /\bbzero\(/) {
2790 ERROR
("use memset() instead of bzero()\n" . $herecurr);
2792 my $non_exit_glib_asserts = qr{g_assert_cmpstr
|
2803 g_test_assert_expected_messages
|
2804 g_test_trap_assert_passed
|
2805 g_test_trap_assert_stdout
|
2806 g_test_trap_assert_stdout_unmatched
|
2807 g_test_trap_assert_stderr
|
2808 g_test_trap_assert_stderr_unmatched
}x
;
2809 if ($realfile !~ /^tests\// &&
2810 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2811 ERROR
("Use g_assert or g_assert_not_reached\n". $herecurr);
2815 # If we have no input at all, then there is nothing to report on
2816 # so just keep quiet.
2817 if ($#rawlines == -1) {
2821 # In mailback mode only produce a report in the negative, for
2822 # things that appear to be patches.
2823 if ($mailback && ($clean == 1 || !$is_patch)) {
2827 # This is not a patch, and we are are in 'no-patch' mode so
2829 if (!$chk_patch && !$is_patch) {
2834 ERROR
("Does not appear to be a unified-diff format patch\n");
2836 if ($is_patch && $chk_signoff && $signoff == 0) {
2837 ERROR
("Missing Signed-off-by: line(s)\n");
2840 print report_dump
();
2841 if ($summary && !($clean == 1 && $quiet == 1)) {
2842 print "$filename " if ($summary_file);
2843 print "total: $cnt_error errors, $cnt_warn warnings, " .
2844 "$cnt_lines lines checked\n";
2845 print "\n" if ($quiet == 0);
2849 # If there were whitespace errors which cleanpatch can fix
2850 # then suggest that.
2851 # if ($rpt_cleaners) {
2852 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2853 # print " scripts/cleanfile\n\n";
2857 if ($clean == 1 && $quiet == 0) {
2858 print "$vname has no obvious style problems and is ready for submission.\n"
2860 if ($clean == 0 && $quiet == 0) {
2861 print "$vname has style problems, please review. If any of these errors\n";
2862 print "are false positives report them to the maintainer, see\n";
2863 print "CHECKPATCH in MAINTAINERS.\n";
2866 return ($no_warnings ?
$clean : $cnt_error == 0);