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 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
228 [\x09\x0A\x0D\x20-\x7E] # ASCII
229 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
230 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
231 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
232 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
233 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
234 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
235 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
238 # There are still some false positives, but this catches most
240 our $typeTypedefs = qr{(?x
:
241 [A
-Z
][A
-Z\d_
]*[a
-z
][A
-Za
-z\d_
]* # camelcase
242 | [A
-Z
][A
-Z\d_
]*AIOCB
# all uppercase
243 | [A
-Z
][A
-Z\d_
]*CPU
# all uppercase
244 | QEMUBH
# all uppercase
249 qr{(?:unsigned\s+)?char},
250 qr{(?:unsigned\s+)?short},
251 qr{(?:unsigned\s+)?int},
252 qr{(?:unsigned\s+)?long},
253 qr{(?:unsigned\s+)?long\s+int},
254 qr{(?:unsigned\s+)?long\s+long},
255 qr{(?:unsigned\s+)?long\s+long\s+int},
264 qr{${Ident}_handler
},
265 qr{${Ident}_handler_fn
},
266 qr{target_(?:u)?long},
271 # This can be modified by sub possible. Since it can be empty, be careful
272 # about regexes that always match, because they can cause infinite loops.
273 our @modifierList = (
277 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
278 if (@modifierList > 0) {
279 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
280 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
282 $Modifier = qr{(?:$Attribute|$Sparse)};
285 (?
:$Modifier\s
+|const\s
+)*
287 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)|
291 (?
:\s
+$Modifier|\s
+const
)*
295 (?
:[\s\
*]+\s
*const
|[\s\
*]+|(?
:\s
*\
[\s
*\
])+)?
296 (?
:\s
+$Inline|\s
+$Modifier)*
298 $Declare = qr{(?:$Storage\s+)?$Type};
302 $chk_signoff = 0 if ($file);
310 open($HASH, "-|", "git", "log", "--format=%H", $ARGV[0]) ||
311 die "$P: git log --format=%H $ARGV[0] failed - $!\n";
320 die "$P: no revisions returned for revlist '$chk_branch'\n"
323 for my $hash (@patches) {
325 open($FILE, '-|', "git", "show", $hash) ||
326 die "$P: git show $hash - $!\n";
333 if (!process
($hash)) {
340 for my $filename (@ARGV) {
343 open($FILE, '-|', "diff -u /dev/null $filename") ||
344 die "$P: $filename: diff failed - $!\n";
345 } elsif ($filename eq '-') {
346 open($FILE, '<&STDIN');
348 open($FILE, '<', "$filename") ||
349 die "$P: $filename: open failed - $!\n";
351 if ($filename eq '-') {
352 $vname = 'Your patch';
361 if (!process
($filename)) {
371 sub top_of_kernel_tree
{
375 "COPYING", "MAINTAINERS", "Makefile",
376 "README", "docs", "VERSION",
380 foreach my $check (@tree_check) {
381 if (! -e
$root . '/' . $check) {
393 for my $c (split(//, $str)) {
397 for (; ($n % 8) != 0; $n++) {
409 (my $res = shift) =~ tr/\t/ /c;
416 # Drop the diff line leader and expand tabs
418 $line = expand_tabs
($line);
420 # Pick the indent from the front of the line.
421 my ($white) = ($line =~ /^(\s*)/);
423 return (length($line), length($white));
426 my $sanitise_quote = '';
428 sub sanitise_line_reset
{
429 my ($in_comment) = @_;
432 $sanitise_quote = '*/';
434 $sanitise_quote = '';
447 # Always copy over the diff marker.
448 $res = substr($line, 0, 1);
450 for ($off = 1; $off < length($line); $off++) {
451 $c = substr($line, $off, 1);
453 # Comments we are wacking completely including the begin
454 # and end, all to $;.
455 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
456 $sanitise_quote = '*/';
458 substr($res, $off, 2, "$;$;");
462 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
463 $sanitise_quote = '';
464 substr($res, $off, 2, "$;$;");
468 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
469 $sanitise_quote = '//';
471 substr($res, $off, 2, $sanitise_quote);
476 # A \ in a string means ignore the next character.
477 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
479 substr($res, $off, 2, 'XX');
484 if ($c eq "'" || $c eq '"') {
485 if ($sanitise_quote eq '') {
486 $sanitise_quote = $c;
488 substr($res, $off, 1, $c);
490 } elsif ($sanitise_quote eq $c) {
491 $sanitise_quote = '';
495 #print "c<$c> SQ<$sanitise_quote>\n";
496 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
497 substr($res, $off, 1, $;);
498 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
499 substr($res, $off, 1, $;);
500 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
501 substr($res, $off, 1, 'X');
503 substr($res, $off, 1, $c);
507 if ($sanitise_quote eq '//') {
508 $sanitise_quote = '';
511 # The pathname on a #include may be surrounded by '<' and '>'.
512 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
513 my $clean = 'X' x
length($1);
514 $res =~ s@\
<.*\
>@
<$clean>@
;
516 # The whole of a #error is a string.
517 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
518 my $clean = 'X' x
length($1);
519 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
525 sub ctx_statement_block
{
526 my ($linenr, $remain, $off) = @_;
527 my $line = $linenr - 1;
544 @stack = (['', 0]) if ($#stack == -1);
546 #warn "CSB: blk<$blk> remain<$remain>\n";
547 # If we are about to drop off the end, pull in more
550 for (; $remain > 0; $line++) {
551 last if (!defined $lines[$line]);
552 next if ($lines[$line] =~ /^-/);
555 $blk .= $lines[$line] . "\n";
560 # Bail if there is no further context.
561 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
567 $c = substr($blk, $off, 1);
568 $remainder = substr($blk, $off);
570 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
572 # Handle nested #if/#else.
573 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
574 push(@stack, [ $type, $level ]);
575 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
576 ($type, $level) = @
{$stack[$#stack - 1]};
577 } elsif ($remainder =~ /^#\s*endif\b/) {
578 ($type, $level) = @
{pop(@stack)};
581 # Statement ends at the ';' or a close '}' at the
583 if ($level == 0 && $c eq ';') {
587 # An else is really a conditional as long as its not else if
588 if ($level == 0 && $coff_set == 0 &&
589 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
590 $remainder =~ /^(else)(?:\s|{)/ &&
591 $remainder !~ /^else\s+if\b/) {
592 $coff = $off + length($1) - 1;
594 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
595 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
598 if (($type eq '' || $type eq '(') && $c eq '(') {
602 if ($type eq '(' && $c eq ')') {
604 $type = ($level != 0)?
'(' : '';
606 if ($level == 0 && $coff < $soff) {
609 #warn "CSB: mark coff<$coff>\n";
612 if (($type eq '' || $type eq '{') && $c eq '{') {
616 if ($type eq '{' && $c eq '}') {
618 $type = ($level != 0)?
'{' : '';
621 if (substr($blk, $off + 1, 1) eq ';') {
629 # We are truly at the end, so shuffle to the next line.
636 my $statement = substr($blk, $soff, $off - $soff + 1);
637 my $condition = substr($blk, $soff, $coff - $soff + 1);
639 #warn "STATEMENT<$statement>\n";
640 #warn "CONDITION<$condition>\n";
642 #print "coff<$coff> soff<$off> loff<$loff>\n";
644 return ($statement, $condition,
645 $line, $remain + 1, $off - $loff + 1, $level);
648 sub statement_lines
{
651 # Strip the diff line prefixes and rip blank lines at start and end.
652 $stmt =~ s/(^|\n)./$1/g;
656 my @stmt_lines = ($stmt =~ /\n/g);
658 return $#stmt_lines + 2;
661 sub statement_rawlines
{
664 my @stmt_lines = ($stmt =~ /\n/g);
666 return $#stmt_lines + 2;
669 sub statement_block_size
{
672 $stmt =~ s/(^|\n)./$1/g;
678 my @stmt_lines = ($stmt =~ /\n/g);
679 my @stmt_statements = ($stmt =~ /;/g);
681 my $stmt_lines = $#stmt_lines + 2;
682 my $stmt_statements = $#stmt_statements + 1;
684 if ($stmt_lines > $stmt_statements) {
687 return $stmt_statements;
691 sub ctx_statement_full
{
692 my ($linenr, $remain, $off) = @_;
693 my ($statement, $condition, $level);
697 # Grab the first conditional/block pair.
698 ($statement, $condition, $linenr, $remain, $off, $level) =
699 ctx_statement_block
($linenr, $remain, $off);
700 #print "F: c<$condition> s<$statement> remain<$remain>\n";
701 push(@chunks, [ $condition, $statement ]);
702 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
703 return ($level, $linenr, @chunks);
706 # Pull in the following conditional/block pairs and see if they
707 # could continue the statement.
709 ($statement, $condition, $linenr, $remain, $off, $level) =
710 ctx_statement_block
($linenr, $remain, $off);
711 #print "C: c<$condition> s<$statement> remain<$remain>\n";
712 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
714 push(@chunks, [ $condition, $statement ]);
717 return ($level, $linenr, @chunks);
721 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
723 my $start = $linenr - 1;
730 my @stack = ($level);
731 for ($line = $start; $remain > 0; $line++) {
732 next if ($rawlines[$line] =~ /^-/);
735 $blk .= $rawlines[$line];
737 # Handle nested #if/#else.
738 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
739 push(@stack, $level);
740 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
741 $level = $stack[$#stack - 1];
742 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
743 $level = pop(@stack);
746 foreach my $c (split(//, $lines[$line])) {
747 ##print "C<$c>L<$level><$open$close>O<$off>\n";
753 if ($c eq $close && $level > 0) {
755 last if ($level == 0);
756 } elsif ($c eq $open) {
761 if (!$outer || $level <= 1) {
762 push(@res, $rawlines[$line]);
765 last if ($level == 0);
768 return ($level, @res);
770 sub ctx_block_outer
{
771 my ($linenr, $remain) = @_;
773 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
777 my ($linenr, $remain) = @_;
779 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
783 my ($linenr, $remain, $off) = @_;
785 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
788 sub ctx_block_level
{
789 my ($linenr, $remain) = @_;
791 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
793 sub ctx_statement_level
{
794 my ($linenr, $remain, $off) = @_;
796 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
799 sub ctx_locate_comment
{
800 my ($first_line, $end_line) = @_;
802 # Catch a comment on the end of the line itself.
803 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
804 return $current_comment if (defined $current_comment);
806 # Look through the context and try and figure out if there is a
809 $current_comment = '';
810 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
811 my $line = $rawlines[$linenr - 1];
813 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
816 if ($line =~ m@
/\
*@
) {
819 if (!$in_comment && $current_comment ne '') {
820 $current_comment = '';
822 $current_comment .= $line . "\n" if ($in_comment);
823 if ($line =~ m@\
*/@
) {
828 chomp($current_comment);
829 return($current_comment);
831 sub ctx_has_comment
{
832 my ($first_line, $end_line) = @_;
833 my $cmt = ctx_locate_comment
($first_line, $end_line);
835 ##print "LINE: $rawlines[$end_line - 1 ]\n";
836 ##print "CMMT: $cmt\n";
842 my ($linenr, $cnt) = @_;
844 my $offset = $linenr - 1;
849 $line = $rawlines[$offset++];
850 next if (defined($line) && $line =~ /^-/);
862 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
865 $coded = sprintf("^%c", unpack('C', $2) + 64);
874 my $av_preprocessor = 0;
880 $av_preprocessor = 0;
882 @av_paren_type = ('E');
883 $av_pend_colon = 'O';
886 sub annotate_values
{
887 my ($stream, $type) = @_;
890 my $var = '_' x
length($stream);
893 print "$stream\n" if ($dbg_values > 1);
895 while (length($cur)) {
896 @av_paren_type = ('E') if ($#av_paren_type < 0);
897 print " <" . join('', @av_paren_type) .
898 "> <$type> <$av_pending>" if ($dbg_values > 1);
899 if ($cur =~ /^(\s+)/o) {
900 print "WS($1)\n" if ($dbg_values > 1);
901 if ($1 =~ /\n/ && $av_preprocessor) {
902 $type = pop(@av_paren_type);
903 $av_preprocessor = 0;
906 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
907 print "CAST($1)\n" if ($dbg_values > 1);
908 push(@av_paren_type, $type);
911 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
912 print "DECLARE($1)\n" if ($dbg_values > 1);
915 } elsif ($cur =~ /^($Modifier)\s*/) {
916 print "MODIFIER($1)\n" if ($dbg_values > 1);
919 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
920 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
921 $av_preprocessor = 1;
922 push(@av_paren_type, $type);
928 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
929 print "UNDEF($1)\n" if ($dbg_values > 1);
930 $av_preprocessor = 1;
931 push(@av_paren_type, $type);
933 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
934 print "PRE_START($1)\n" if ($dbg_values > 1);
935 $av_preprocessor = 1;
937 push(@av_paren_type, $type);
938 push(@av_paren_type, $type);
941 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
942 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
943 $av_preprocessor = 1;
945 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
949 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
950 print "PRE_END($1)\n" if ($dbg_values > 1);
952 $av_preprocessor = 1;
954 # Assume all arms of the conditional end as this
955 # one does, and continue as if the #endif was not here.
957 push(@av_paren_type, $type);
960 } elsif ($cur =~ /^(\\\n)/o) {
961 print "PRECONT($1)\n" if ($dbg_values > 1);
963 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
964 print "ATTR($1)\n" if ($dbg_values > 1);
968 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
969 print "SIZEOF($1)\n" if ($dbg_values > 1);
975 } elsif ($cur =~ /^(if|while|for)\b/o) {
976 print "COND($1)\n" if ($dbg_values > 1);
980 } elsif ($cur =~/^(case)/o) {
981 print "CASE($1)\n" if ($dbg_values > 1);
982 $av_pend_colon = 'C';
985 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
986 print "KEYWORD($1)\n" if ($dbg_values > 1);
989 } elsif ($cur =~ /^(\()/o) {
990 print "PAREN('$1')\n" if ($dbg_values > 1);
991 push(@av_paren_type, $av_pending);
995 } elsif ($cur =~ /^(\))/o) {
996 my $new_type = pop(@av_paren_type);
997 if ($new_type ne '_') {
999 print "PAREN('$1') -> $type\n"
1000 if ($dbg_values > 1);
1002 print "PAREN('$1')\n" if ($dbg_values > 1);
1005 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1006 print "FUNC($1)\n" if ($dbg_values > 1);
1010 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1011 if (defined $2 && $type eq 'C' || $type eq 'T') {
1012 $av_pend_colon = 'B';
1013 } elsif ($type eq 'E') {
1014 $av_pend_colon = 'L';
1016 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1019 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1020 print "IDENT($1)\n" if ($dbg_values > 1);
1023 } elsif ($cur =~ /^($Assignment)/o) {
1024 print "ASSIGN($1)\n" if ($dbg_values > 1);
1027 } elsif ($cur =~/^(;|{|})/) {
1028 print "END($1)\n" if ($dbg_values > 1);
1030 $av_pend_colon = 'O';
1032 } elsif ($cur =~/^(,)/) {
1033 print "COMMA($1)\n" if ($dbg_values > 1);
1036 } elsif ($cur =~ /^(\?)/o) {
1037 print "QUESTION($1)\n" if ($dbg_values > 1);
1040 } elsif ($cur =~ /^(:)/o) {
1041 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1043 substr($var, length($res), 1, $av_pend_colon);
1044 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1049 $av_pend_colon = 'O';
1051 } elsif ($cur =~ /^(\[)/o) {
1052 print "CLOSE($1)\n" if ($dbg_values > 1);
1055 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1058 print "OPV($1)\n" if ($dbg_values > 1);
1065 substr($var, length($res), 1, $variant);
1068 } elsif ($cur =~ /^($Operators)/o) {
1069 print "OP($1)\n" if ($dbg_values > 1);
1070 if ($1 ne '++' && $1 ne '--') {
1074 } elsif ($cur =~ /(^.)/o) {
1075 print "C($1)\n" if ($dbg_values > 1);
1078 $cur = substr($cur, length($1));
1079 $res .= $type x
length($1);
1083 return ($res, $var);
1087 my ($possible, $line) = @_;
1088 my $notPermitted = qr{(?
:
1105 ^(?
:typedef
|struct
|enum
)\b
1107 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1108 if ($possible !~ $notPermitted) {
1109 # Check for modifiers.
1110 $possible =~ s/\s*$Storage\s*//g;
1111 $possible =~ s/\s*$Sparse\s*//g;
1112 if ($possible =~ /^\s*$/) {
1114 } elsif ($possible =~ /\s/) {
1115 $possible =~ s/\s*$Type\s*//g;
1116 for my $modifier (split(' ', $possible)) {
1117 if ($modifier !~ $notPermitted) {
1118 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1119 push(@modifierList, $modifier);
1124 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1125 push(@typeList, $possible);
1129 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1136 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1139 my $line = $prefix . $_[0];
1141 $line = (split('\n', $line))[0] . "\n" if ($terse);
1143 push(our @report, $line);
1151 if (report
("ERROR: $_[0]\n")) {
1157 if (report
("WARNING: $_[0]\n")) {
1164 my $filename = shift;
1170 my $stashrawline="";
1187 # Trace the real file/line as we go.
1193 my $comment_edge = 0;
1197 my $prev_values = 'E';
1200 my %suppress_ifbraces;
1201 my %suppress_whiletrailers;
1202 my %suppress_export;
1204 # Pre-scan the patch sanitizing the lines.
1206 sanitise_line_reset
();
1208 foreach my $rawline (@rawlines) {
1212 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1221 # Guestimate if this is a continuing comment. Run
1222 # the context looking for a comment "edge". If this
1223 # edge is a close comment then we must be in a comment
1227 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1228 next if (defined $rawlines[$ln - 1] &&
1229 $rawlines[$ln - 1] =~ /^-/);
1231 #print "RAW<$rawlines[$ln - 1]>\n";
1232 last if (!defined $rawlines[$ln - 1]);
1233 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1234 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1239 if (defined $edge && $edge eq '*/') {
1243 # Guestimate if this is a continuing comment. If this
1244 # is the start of a diff block and this line starts
1245 # ' *' then it is very likely a comment.
1246 if (!defined $edge &&
1247 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1252 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1253 sanitise_line_reset
($in_comment);
1255 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1256 # Standardise the strings and chars within the input to
1257 # simplify matching -- only bother with positive lines.
1258 $line = sanitise_line
($rawline);
1260 push(@lines, $line);
1263 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1268 #print "==>$rawline\n";
1269 #print "-->$line\n";
1276 foreach my $line (@lines) {
1279 my $rawline = $rawlines[$linenr - 1];
1281 #extract the line range in the file after the patch is applied
1282 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1284 $first_line = $linenr + 1;
1294 %suppress_ifbraces = ();
1295 %suppress_whiletrailers = ();
1296 %suppress_export = ();
1299 # track the line number as we move through the hunk, note that
1300 # new versions of GNU diff omit the leading space on completely
1301 # blank context lines so we need to count that too.
1302 } elsif ($line =~ /^( |\+|$)/) {
1304 $realcnt-- if ($realcnt != 0);
1306 # Measure the line length and indent.
1307 ($length, $indent) = line_stats
($rawline);
1309 # Track the previous line.
1310 ($prevline, $stashline) = ($stashline, $line);
1311 ($previndent, $stashindent) = ($stashindent, $indent);
1312 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1314 #warn "line<$line>\n";
1316 } elsif ($realcnt == 1) {
1320 my $hunk_line = ($realcnt != 0);
1322 #make up the handle for any error we report on this line
1323 $prefix = "$filename:$realline: " if ($emacs && $file);
1324 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1326 $here = "#$linenr: " if (!$file);
1327 $here = "#$realline: " if ($file);
1329 # extract the filename as it passes
1330 if ($line =~ /^diff --git.*?(\S+)$/) {
1332 $realfile =~ s@
^([^/]*)/@@
;
1334 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1336 $realfile =~ s@
^([^/]*)/@@
;
1339 if (!$file && $tree && $p1_prefix ne '' &&
1340 -e
"$root/$p1_prefix") {
1341 WARN
("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1347 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1349 my $hereline = "$here\n$rawline\n";
1350 my $herecurr = "$here\n$rawline\n";
1351 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1353 $cnt_lines++ if ($realcnt != 0);
1355 # Check for incorrect file permissions
1356 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1357 my $permhere = $here . "FILE: $realfile\n";
1358 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1359 ERROR
("do not set execute permissions for source files\n" . $permhere);
1363 # Accept git diff extended headers as valid patches
1364 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\
.\
-]+\s
*$/) {
1368 #check the patch for a signoff:
1369 if ($line =~ /^\s*signed-off-by:/i) {
1370 # This is a signoff, if ugly, so do not double report.
1372 if (!($line =~ /^\s*Signed-off-by:/)) {
1373 ERROR
("The correct form is \"Signed-off-by\"\n" .
1376 if ($line =~ /^\s*signed-off-by:\S/i) {
1377 ERROR
("space required after Signed-off-by:\n" .
1382 # Check for wrappage within a valid hunk of the file
1383 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1384 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1385 $herecurr) if (!$emitted_corrupt++);
1388 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1389 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1390 $rawline !~ m/^$UTF8*$/) {
1391 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1393 my $blank = copy_spacing
($rawline);
1394 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1395 my $hereptr = "$hereline$ptr\n";
1397 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1400 # ignore non-hunk lines and lines being removed
1401 next if (!$hunk_line || $line =~ /^-/);
1403 # ignore files that are being periodically imported from Linux
1404 next if ($realfile =~ /^(linux-headers|include\/standard
-headers
)\
//);
1406 #trailing whitespace
1407 if ($line =~ /^\+.*\015/) {
1408 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1409 ERROR
("DOS line endings\n" . $herevet);
1411 } elsif ($realfile =~ /^docs\/.+\
.txt
/ ||
1412 $realfile =~ /^docs\/.+\
.md
/) {
1413 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1414 # TODO: properly check we're in a code block
1415 # (surrounding text is 4-column aligned)
1416 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1417 ERROR
("code blocks in documentation should have " .
1418 "empty lines with exactly 4 columns of " .
1419 "whitespace\n" . $herevet);
1421 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1422 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1423 ERROR
("trailing whitespace\n" . $herevet);
1427 # checks for trace-events files
1428 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1429 if ($rawline =~ /%[-+ 0]*#/) {
1430 ERROR
("Don't use '#' flag of printf format ('%#') in " .
1431 "trace-events, use '0x' prefix instead\n" . $herecurr);
1434 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1436 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
1437 my $tmpline = $rawline;
1438 $tmpline =~ s/($hex[.:\/ ])+$hex//g
;
1440 if ($tmpline =~ /(?<!0x)$hex/) {
1441 ERROR
("Hex numbers must be prefixed with '0x'\n" .
1447 # check we are in a valid source file if not then ignore this hunk
1448 next if ($realfile !~ /$SrcFile/);
1450 #90 column limit; exempt URLs, if no other words on line
1451 if ($line =~ /^\+/ &&
1452 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1453 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1457 ERROR
("line over 90 characters\n" . $herecurr);
1459 WARN
("line over 80 characters\n" . $herecurr);
1463 # check for spaces before a quoted newline
1464 if ($rawline =~ /^.*\".*\s\\n/) {
1465 ERROR
("unnecessary whitespace before a quoted newline\n" . $herecurr);
1468 # check for adding lines without a newline.
1469 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1470 ERROR
("adding a line without newline at end of file\n" . $herecurr);
1473 # check for RCS/CVS revision markers
1474 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1475 ERROR
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1478 # tabs are only allowed in assembly source code, and in
1479 # some scripts we imported from other projects.
1480 next if ($realfile =~ /\.(s|S)$/);
1481 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1483 if ($rawline =~ /^\+.*\t/) {
1484 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1485 ERROR
("code indent should never use tabs\n" . $herevet);
1489 # check we are in a valid C source file if not then ignore this hunk
1490 next if ($realfile !~ /\.(h|c|cpp)$/);
1492 # Check for potential 'bare' types
1493 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1495 if ($realcnt && $line =~ /.\s*\S/) {
1496 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1497 ctx_statement_block
($linenr, $realcnt, 0);
1498 $stat =~ s/\n./\n /g;
1499 $cond =~ s/\n./\n /g;
1501 # Find the real next line.
1502 $realline_next = $line_nr_next;
1503 if (defined $realline_next &&
1504 (!defined $lines[$realline_next - 1] ||
1505 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1512 # Ignore goto labels.
1513 if ($s =~ /$Ident:\*$/s) {
1515 # Ignore functions being called
1516 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1518 } elsif ($s =~ /^.\s*else\b/s) {
1520 # declarations always start with types
1521 } 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) {
1524 possible
($type, "A:" . $s);
1526 # definitions in global scope can only start with types
1527 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1528 possible
($1, "B:" . $s);
1531 # any (foo ... *) is a pointer cast, and foo is a type
1532 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1533 possible
($1, "C:" . $s);
1536 # Check for any sort of function declaration.
1537 # int foo(something bar, other baz);
1538 # void (*store_gdt)(x86_descr_ptr *);
1539 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1540 my ($name_len) = length($1);
1543 substr($ctx, 0, $name_len + 1, '');
1544 $ctx =~ s/\)[^\)]*$//;
1546 for my $arg (split(/\s*,\s*/, $ctx)) {
1547 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1549 possible
($1, "D:" . $s);
1557 # Checks which may be anchored in the context.
1560 # Check for switch () and associated case and default
1561 # statements should be at the same indent.
1562 if ($line=~/\bswitch\s*\(.*\)/) {
1565 my @ctx = ctx_block_outer
($linenr, $realcnt);
1567 for my $ctx (@ctx) {
1568 my ($clen, $cindent) = line_stats
($ctx);
1569 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1570 $indent != $cindent) {
1571 $err .= "$sep$ctx\n";
1578 ERROR
("switch and case should be at the same indent\n$hereline$err");
1582 # if/while/etc brace do not go on next line, unless defining a do while loop,
1583 # or if that brace on the next line is for something else
1584 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1585 my $pre_ctx = "$1$2";
1587 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1588 my $ctx_cnt = $realcnt - $#ctx - 1;
1589 my $ctx = join("\n", @ctx);
1591 my $ctx_ln = $linenr;
1592 my $ctx_skip = $realcnt;
1594 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1595 defined $lines[$ctx_ln - 1] &&
1596 $lines[$ctx_ln - 1] =~ /^-/)) {
1597 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1598 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1602 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1603 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1605 # The length of the "previous line" is checked against 80 because it
1606 # includes the + at the beginning of the line (if the actual line has
1607 # 79 or 80 characters, it is no longer possible to add a space and an
1608 # opening brace there)
1609 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1610 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1611 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1612 ERROR
("that open brace { should be on the previous line\n" .
1613 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1615 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1616 $ctx =~ /\)\s*\;\s*$/ &&
1617 defined $lines[$ctx_ln - 1])
1619 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1620 if ($nindent > $indent) {
1621 ERROR
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1622 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1627 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1628 if ($line =~ /while\s*\((0|false)\);/) {
1629 ERROR
("suspicious ; after while (0)\n" . $herecurr);
1632 # Check relative indent for conditionals and blocks.
1633 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1634 my ($s, $c) = ($stat, $cond);
1636 substr($s, 0, length($c), '');
1638 # Make sure we remove the line prefixes as we have
1639 # none on the first line, and are going to readd them
1643 # Find out how long the conditional actually is.
1644 my @newlines = ($c =~ /\n/gs);
1645 my $cond_lines = 1 + $#newlines;
1647 # We want to check the first line inside the block
1648 # starting at the end of the conditional, so remove:
1649 # 1) any blank line termination
1650 # 2) any opening brace { on end of the line
1652 my $continuation = 0;
1654 $s =~ s/^.*\bdo\b//;
1656 if ($s =~ s/^\s*\\//) {
1659 if ($s =~ s/^\s*?\n//) {
1664 # Also ignore a loop construct at the end of a
1665 # preprocessor statement.
1666 if (($prevline =~ /^.\s*#\s*define\s/ ||
1667 $prevline =~ /\\\s*$/) && $continuation == 0) {
1673 while ($cond_ptr != $cond_lines) {
1674 $cond_ptr = $cond_lines;
1676 # If we see an #else/#elif then the code
1678 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1683 # 1) blank lines, they should be at 0,
1684 # 2) preprocessor lines, and
1686 if ($continuation ||
1688 $s =~ /^\s*#\s*?/ ||
1689 $s =~ /^\s*$Ident\s*:/) {
1690 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
1691 if ($s =~ s/^.*?\n//) {
1697 my (undef, $sindent) = line_stats
("+" . $s);
1698 my $stat_real = raw_line
($linenr, $cond_lines);
1700 # Check if either of these lines are modified, else
1701 # this is not this patch's fault.
1702 if (!defined($stat_real) ||
1703 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1706 if (defined($stat_real) && $cond_lines > 1) {
1707 $stat_real = "[...]\n$stat_real";
1710 #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";
1712 if ($check && (($sindent % 4) != 0 ||
1713 ($sindent <= $indent && $s ne ''))) {
1714 ERROR
("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1718 # Track the 'values' across context and added lines.
1719 my $opline = $line; $opline =~ s/^./ /;
1720 my ($curr_values, $curr_vars) =
1721 annotate_values
($opline . "\n", $prev_values);
1722 $curr_values = $prev_values . $curr_values;
1724 my $outline = $opline; $outline =~ s/\t/ /g;
1725 print "$linenr > .$outline\n";
1726 print "$linenr > $curr_values\n";
1727 print "$linenr > $curr_vars\n";
1729 $prev_values = substr($curr_values, -1);
1731 #ignore lines not being added
1732 if ($line=~/^[^\+]/) {next;}
1734 # TEST: allow direct testing of the type matcher.
1736 if ($line =~ /^.\s*$Declare\s*$/) {
1737 ERROR
("TEST: is type\n" . $herecurr);
1738 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1739 ERROR
("TEST: is not type ($1 is)\n". $herecurr);
1743 # TEST: allow direct testing of the attribute matcher.
1745 if ($line =~ /^.\s*$Modifier\s*$/) {
1746 ERROR
("TEST: is attr\n" . $herecurr);
1747 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1748 ERROR
("TEST: is not attr ($1 is)\n". $herecurr);
1753 # check for initialisation to aggregates open brace on the next line
1754 if ($line =~ /^.\s*\{/ &&
1755 $prevline =~ /(?:^|[^=])=\s*$/) {
1756 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1760 # Checks which are anchored on the added line.
1763 # check for malformed paths in #include statements (uses RAW line)
1764 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1766 if ($path =~ m{//}) {
1767 ERROR
("malformed #include filename\n" .
1772 # no C99 // comments
1773 if ($line =~ m{//}) {
1774 ERROR
("do not use C99 // comments\n" . $herecurr);
1776 # Remove C99 comments.
1778 $opline =~ s@
//.*@@
;
1780 # check for global initialisers.
1781 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1782 ERROR
("do not initialise globals to 0 or NULL\n" .
1785 # check for static initialisers.
1786 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1787 ERROR
("do not initialise statics to 0 or NULL\n" .
1791 # * goes on variable not on type
1793 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1794 my ($from, $to) = ($1, $1);
1796 # Should start with a space.
1797 $to =~ s/^(\S)/ $1/;
1798 # Should not end with a space.
1800 # '*'s should not have spaces between.
1801 while ($to =~ s/\*\s+\*/\*\*/) {
1804 #print "from<$from> to<$to>\n";
1806 ERROR
("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1808 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1809 my ($from, $to, $ident) = ($1, $1, $2);
1811 # Should start with a space.
1812 $to =~ s/^(\S)/ $1/;
1813 # Should not end with a space.
1815 # '*'s should not have spaces between.
1816 while ($to =~ s/\*\s+\*/\*\*/) {
1818 # Modifiers should have spaces.
1819 $to =~ s/(\b$Modifier$)/$1 /;
1821 #print "from<$from> to<$to> ident<$ident>\n";
1822 if ($from ne $to && $ident !~ /^$Modifier$/) {
1823 ERROR
("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1827 # function brace can't be on same line, except for #defines of do while,
1828 # or if closed on same line
1829 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1830 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1831 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1834 # open braces for enum, union and struct go on the same line.
1835 if ($line =~ /^.\s*\{/ &&
1836 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1837 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1840 # missing space after union, struct or enum definition
1841 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1842 ERROR
("missing space after $1 definition\n" . $herecurr);
1845 # check for spacing round square brackets; allowed:
1846 # 1. with a type on the left -- int [] a;
1847 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1848 # 3. inside a curly brace -- = { [0...10] = 5 }
1849 # 4. after a comma -- [1] = 5, [2] = 6
1850 # 5. in a macro definition -- #define abc(x) [x] = y
1851 while ($line =~ /(.*?\s)\[/g) {
1852 my ($where, $prefix) = ($-[1], $1);
1853 if ($prefix !~ /$Type\s+$/ &&
1854 ($where != 0 || $prefix !~ /^.\s+$/) &&
1855 $prefix !~ /{\s+$/ &&
1856 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1857 $prefix !~ /,\s+$/) {
1858 ERROR
("space prohibited before open square bracket '['\n" . $herecurr);
1862 # check for spaces between functions and their parentheses.
1863 while ($line =~ /($Ident)\s+\(/g) {
1865 my $ctx_before = substr($line, 0, $-[1]);
1866 my $ctx = "$ctx_before$name";
1868 # Ignore those directives where spaces _are_ permitted.
1870 if|for|while|switch
|return|case
|
1871 volatile
|__volatile__
|coroutine_fn
|
1872 __attribute__
|format
|__extension__
|
1876 # Ignore 'catch (...)' in C++
1877 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1879 # cpp #define statements have non-optional spaces, ie
1880 # if there is a space between the name and the open
1881 # parenthesis it is simply not a parameter group.
1882 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1884 # cpp #elif statement condition may start with a (
1885 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1887 # If this whole things ends with a type its most
1888 # likely a typedef for a function.
1889 } elsif ($ctx =~ /$Type$/) {
1892 ERROR
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1895 # Check operator spacing.
1896 if (!($line=~/\#\s*include/)) {
1898 <<=|>>=|<=|>=|==|!=|
1899 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1900 =>|->|<<|>>|<|>|=|!|~|
1901 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
1904 my @elements = split(/($ops|;)/, $opline);
1907 my $blank = copy_spacing
($opline);
1909 for (my $n = 0; $n < $#elements; $n += 2) {
1910 $off += length($elements[$n]);
1912 # Pick up the preceding and succeeding characters.
1913 my $ca = substr($opline, 0, $off);
1915 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1916 $cc = substr($opline, $off + length($elements[$n + 1]));
1918 my $cb = "$ca$;$cc";
1921 $a = 'V' if ($elements[$n] ne '');
1922 $a = 'W' if ($elements[$n] =~ /\s$/);
1923 $a = 'C' if ($elements[$n] =~ /$;$/);
1924 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1925 $a = 'O' if ($elements[$n] eq '');
1926 $a = 'E' if ($ca =~ /^\s*$/);
1928 my $op = $elements[$n + 1];
1931 if (defined $elements[$n + 2]) {
1932 $c = 'V' if ($elements[$n + 2] ne '');
1933 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1934 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1935 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1936 $c = 'O' if ($elements[$n + 2] eq '');
1937 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1942 my $ctx = "${a}x${c}";
1944 my $at = "(ctx:$ctx)";
1946 my $ptr = substr($blank, 0, $off) . "^";
1947 my $hereptr = "$hereline$ptr\n";
1949 # Pull out the value of this operator.
1950 my $op_type = substr($curr_values, $off + 1, 1);
1952 # Get the full operator variant.
1953 my $opv = $op . substr($curr_vars, $off, 1);
1955 # Ignore operators passed as parameters.
1956 if ($op_type ne 'V' &&
1957 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1960 # } elsif ($op =~ /^$;+$/) {
1962 # ; should have either the end of line or a space or \ after it
1963 } elsif ($op eq ';') {
1964 if ($ctx !~ /.x[WEBC]/ &&
1965 $cc !~ /^\\/ && $cc !~ /^;/) {
1966 ERROR
("space required after that '$op' $at\n" . $hereptr);
1970 } elsif ($op eq '//') {
1972 # Ignore : used in class declaration in C++
1973 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
1974 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
1978 # : when part of a bitfield
1979 } elsif ($op eq '->' || $opv eq ':B') {
1980 if ($ctx =~ /Wx.|.xW/) {
1981 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
1984 # , must have a space on the right.
1985 # not required when having a single },{ on one line
1986 } elsif ($op eq ',') {
1987 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
1988 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
1989 ERROR
("space required after that '$op' $at\n" . $hereptr);
1992 # '*' as part of a type definition -- reported already.
1993 } elsif ($opv eq '*_') {
1994 #warn "'*' is part of type\n";
1996 # unary operators should have a space before and
1997 # none after. May be left adjacent to another
1998 # unary operator, or a cast
1999 } elsif ($op eq '!' || $op eq '~' ||
2000 $opv eq '*U' || $opv eq '-U' ||
2001 $opv eq '&U' || $opv eq '&&U') {
2002 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2003 # '~' used as a name of Destructor
2005 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2006 ERROR
("space required before that '$op' $at\n" . $hereptr);
2008 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2009 # A unary '*' may be const
2011 } elsif ($ctx =~ /.xW/) {
2012 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2015 # unary ++ and unary -- are allowed no space on one side.
2016 } elsif ($op eq '++' or $op eq '--') {
2017 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2018 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
2020 if ($ctx =~ /Wx[BE]/ ||
2021 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2022 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2024 if ($ctx =~ /ExW/) {
2025 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2028 # A colon needs no spaces before when it is
2029 # terminating a case value or a label.
2030 } elsif ($opv eq ':C' || $opv eq ':L') {
2031 if ($ctx =~ /Wx./) {
2032 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2035 # All the others need spaces both sides.
2036 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2039 if ($realfile =~ /\.cpp|\.h$/) {
2040 # Ignore template arguments <...> in C++
2041 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2051 # Ignore email addresses <foo@bar>
2053 $cc =~ /^\S+\@\S+>/) ||
2055 $ca =~ /<\S+\@\S+$/))
2061 if (($opv eq ':O' && $ca =~ /\?$/) ||
2062 ($op eq '?' && $cc =~ /^:/)) {
2067 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
2070 $off += length($elements[$n + 1]);
2074 #need space before brace following if, while, etc
2075 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2077 ERROR
("space required before the open brace '{'\n" . $herecurr);
2080 # closing brace should have a space following it when it has anything
2082 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2083 ERROR
("space required after that close brace '}'\n" . $herecurr);
2086 # check spacing on square brackets
2087 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2088 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
2090 if ($line =~ /\s\]/) {
2091 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
2094 # check spacing on parentheses
2095 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2096 $line !~ /for\s*\(\s+;/) {
2097 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
2099 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2100 $line !~ /for\s*\(.*;\s+\)/ &&
2101 $line !~ /:\s+\)/) {
2102 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
2105 # Return is not a function.
2106 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2110 # Flatten any parentheses
2111 $value =~ s/\(/ \(/g;
2112 $value =~ s/\)/\) /g;
2113 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2114 $value !~ /(?
:$Ident|-?
$Constant)\s
*
2116 (?
:$Ident|-?
$Constant)/x
&&
2117 $value =~ s/\([^\(\)]*\)/1/) {
2119 #print "value<$value>\n";
2120 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2121 ERROR
("return is not a function, parentheses are not required\n" . $herecurr);
2123 } elsif ($spacing !~ /\s+/) {
2124 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2127 # Return of what appears to be an errno should normally be -'ve
2128 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2130 if ($name ne 'EOF' && $name ne 'ERROR') {
2131 ERROR
("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2135 # Need a space before open parenthesis after if, while etc
2136 if ($line=~/\b(if|while|for|switch)\(/) {
2137 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2140 # Check for illegal assignment in if conditional -- and check for trailing
2141 # statements after the conditional.
2142 if ($line =~ /do\s*(?!{)/) {
2143 my ($stat_next) = ctx_statement_block
($line_nr_next,
2144 $remain_next, $off_next);
2145 $stat_next =~ s/\n./\n /g;
2146 ##print "stat<$stat> stat_next<$stat_next>\n";
2148 if ($stat_next =~ /^\s*while\b/) {
2149 # If the statement carries leading newlines,
2150 # then count those as offsets.
2152 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2154 statement_rawlines
($whitespace) - 1;
2156 $suppress_whiletrailers{$line_nr_next +
2160 if (!defined $suppress_whiletrailers{$linenr} &&
2161 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2162 my ($s, $c) = ($stat, $cond);
2164 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2165 ERROR
("do not use assignment in if condition\n" . $herecurr);
2168 # Find out what is on the end of the line after the
2170 substr($s, 0, length($c), '');
2172 $s =~ s/$;//g; # Remove any comments
2173 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2174 $c !~ /}\s*while\s*/)
2176 # Find out how long the conditional actually is.
2177 my @newlines = ($c =~ /\n/gs);
2178 my $cond_lines = 1 + $#newlines;
2181 $stat_real = raw_line
($linenr, $cond_lines)
2182 . "\n" if ($cond_lines);
2183 if (defined($stat_real) && $cond_lines > 1) {
2184 $stat_real = "[...]\n$stat_real";
2187 ERROR
("trailing statements should be on next line\n" . $herecurr . $stat_real);
2191 # Check for bitwise tests written as boolean
2203 ERROR
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2206 # if and else should not have general statements after it
2207 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2209 $s =~ s/$;//g; # Remove any comments
2210 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2211 ERROR
("trailing statements should be on next line\n" . $herecurr);
2214 # if should not continue a brace
2215 if ($line =~ /}\s*if\b/) {
2216 ERROR
("trailing statements should be on next line\n" .
2219 # case and default should not have general statements after them
2220 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2222 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2226 ERROR
("trailing statements should be on next line\n" . $herecurr);
2229 # Check for }<nl>else {, these must be at the same
2230 # indent level to be relevant to each other.
2231 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2232 $previndent == $indent) {
2233 ERROR
("else should follow close brace '}'\n" . $hereprev);
2236 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2237 $previndent == $indent) {
2238 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2240 # Find out what is on the end of the line after the
2242 substr($s, 0, length($c), '');
2245 if ($s =~ /^\s*;/) {
2246 ERROR
("while should follow close brace '}'\n" . $hereprev);
2250 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2251 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2252 # print "No studly caps, use _\n";
2253 # print "$herecurr";
2257 #no spaces allowed after \ in define
2258 if ($line=~/\#\s*define.*\\\s$/) {
2259 ERROR
("Whitespace after \\ makes next lines useless\n" . $herecurr);
2262 # multi-statement macros should be enclosed in a do while loop, grab the
2263 # first statement and ensure its the whole macro if its not enclosed
2264 # in a known good container
2265 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2266 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2269 my ($off, $dstat, $dcond, $rest);
2272 my $args = defined($1);
2274 # Find the end of the macro and limit our statement
2276 while ($cnt > 0 && defined $lines[$ln - 1] &&
2277 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2279 $ctx .= $rawlines[$ln - 1] . "\n";
2280 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2283 $ctx .= $rawlines[$ln - 1];
2285 ($dstat, $dcond, $ln, $cnt, $off) =
2286 ctx_statement_block
($linenr, $ln - $linenr + 1, 0);
2287 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2288 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2290 # Extract the remainder of the define (if any) and
2291 # rip off surrounding spaces, and trailing \'s.
2293 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2294 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2295 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2296 $rest .= substr($lines[$ln - 1], $off) . "\n";
2302 $rest =~ s/\\\n.//g;
2306 # Clean up the original statement.
2308 substr($dstat, 0, length($dcond), '');
2310 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2313 $dstat =~ s/\\\n.//g;
2314 $dstat =~ s/^\s*//s;
2315 $dstat =~ s/\s*$//s;
2317 # Flatten any parentheses and braces
2318 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2319 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2320 $dstat =~ s/\[[^\{\}]*\]/1/)
2324 my $exceptions = qr{
2336 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2337 if ($rest ne '' && $rest ne ',') {
2338 if ($rest !~ /while\s*\(/ &&
2339 $dstat !~ /$exceptions/)
2341 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2344 } elsif ($ctx !~ /;/) {
2346 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2347 $dstat !~ /$exceptions/ &&
2348 $dstat !~ /^\.$Ident\s*=/ &&
2349 $dstat =~ /$Operators/)
2351 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2356 # check for missing bracing around if etc
2357 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2358 $line !~ /\#\s*if/) {
2359 my ($level, $endln, @chunks) =
2360 ctx_statement_full
($linenr, $realcnt, 1);
2362 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2363 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2366 if ($#chunks >= 0 && $level == 0) {
2369 my $herectx = $here . "\n";
2370 my $ln = $linenr - 1;
2371 for my $chunk (@chunks) {
2372 my ($cond, $block) = @
{$chunk};
2374 # If the condition carries leading newlines, then count those as offsets.
2375 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2376 my $offset = statement_rawlines
($whitespace) - 1;
2378 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2380 # We have looked at and allowed this specific line.
2381 $suppress_ifbraces{$ln + $offset} = 1;
2383 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2384 $ln += statement_rawlines
($block) - 1;
2386 substr($block, 0, length($cond), '');
2388 my $spaced_block = $block;
2389 $spaced_block =~ s/\n\+/ /g;
2391 $seen++ if ($spaced_block =~ /^\s*\{/);
2393 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2395 if (statement_lines
($cond) > 1) {
2396 print "APW: ALLOWED: cond<$cond>\n"
2400 if ($block =~/\b(?:if|for|while)\b/) {
2401 print "APW: ALLOWED: block<$block>\n"
2405 if (statement_block_size
($block) > 1) {
2406 print "APW: ALLOWED: lines block<$block>\n"
2411 if ($seen != ($#chunks + 1)) {
2412 ERROR
("braces {} are necessary for all arms of this statement\n" . $herectx);
2416 if (!defined $suppress_ifbraces{$linenr - 1} &&
2417 $line =~ /\b(if|while|for|else)\b/ &&
2418 $line !~ /\#\s*if/ &&
2419 $line !~ /\#\s*else/) {
2422 # Check the pre-context.
2423 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2426 if ($line !~ /else/) {
2427 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2433 my ($level, $endln, @chunks) =
2434 ctx_statement_full
($linenr, $realcnt, $-[0]);
2436 # Check the condition.
2437 my ($cond, $block) = @
{$chunks[0]};
2438 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2439 if $dbg_adv_checking;
2440 if (defined $cond) {
2441 substr($block, 0, length($cond), '');
2443 if (statement_lines
($cond) > 1) {
2444 print "APW: ALLOWED: cond<$cond>\n"
2448 if ($block =~/\b(?:if|for|while)\b/) {
2449 print "APW: ALLOWED: block<$block>\n"
2453 if (statement_block_size
($block) > 1) {
2454 print "APW: ALLOWED: lines block<$block>\n"
2458 # Check the post-context.
2459 if (defined $chunks[1]) {
2460 my ($cond, $block) = @
{$chunks[1]};
2461 if (defined $cond) {
2462 substr($block, 0, length($cond), '');
2464 if ($block =~ /^\s*\{/) {
2465 print "APW: ALLOWED: chunk-1 block<$block>\n"
2470 print "DCS: level=$level block<$block> allowed=$allowed\n"
2472 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2473 my $herectx = $here . "\n";;
2474 my $cnt = statement_rawlines
($block);
2476 for (my $n = 0; $n < $cnt; $n++) {
2477 $herectx .= raw_line
($linenr, $n) . "\n";;
2480 ERROR
("braces {} are necessary even for single statement blocks\n" . $herectx);
2484 # no volatiles please
2485 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2486 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ &&
2487 $line !~ /sig_atomic_t/ &&
2488 !ctx_has_comment
($first_line, $linenr)) {
2489 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr;
2494 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2495 ERROR
("if this code is redundant consider removing it\n" .
2499 # check for needless g_free() checks
2500 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2502 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2503 ERROR
("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2507 # warn about #ifdefs in C files
2508 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2509 # print "#ifdef in C files should be avoided\n";
2510 # print "$herecurr";
2514 # warn about spacing in #ifdefs
2515 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2516 ERROR
("exactly one space required after that #$1\n" . $herecurr);
2518 # check for memory barriers without a comment.
2519 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2520 if (!ctx_has_comment
($first_line, $linenr)) {
2521 ERROR
("memory barrier without comment\n" . $herecurr);
2524 # check of hardware specific defines
2525 # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2526 # where they might be necessary.
2527 if ($line =~ m@
^.\s
*\#\s
*if.*\b__@
) {
2528 WARN
("architecture specific defines should be avoided\n" . $herecurr);
2531 # Check that the storage class is at the beginning of a declaration
2532 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2533 ERROR
("storage class should be at the beginning of the declaration\n" . $herecurr)
2536 # check the location of the inline attribute, that it is between
2537 # storage class and type.
2538 if ($line =~ /\b$Type\s+$Inline\b/ ||
2539 $line =~ /\b$Inline\s+$Storage\b/) {
2540 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
2543 # check for sizeof(&)
2544 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2545 ERROR
("sizeof(& should be avoided\n" . $herecurr);
2548 # check for new externs in .c files.
2549 if ($realfile =~ /\.c$/ && defined $stat &&
2550 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2552 my $function_name = $1;
2553 my $paren_space = $2;
2556 if (defined $cond) {
2557 substr($s, 0, length($cond), '');
2559 if ($s =~ /^\s*;/ &&
2560 $function_name ne 'uninitialized_var')
2562 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2565 if ($paren_space =~ /\n/) {
2566 ERROR
("arguments for function declarations should follow identifier\n" . $herecurr);
2569 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2570 $stat =~ /^.\s*extern\s+/)
2572 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2575 # check for pointless casting of g_malloc return
2576 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2578 ERROR
("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2580 ERROR
("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2584 # check for gcc specific __FUNCTION__
2585 if ($line =~ /__FUNCTION__/) {
2586 ERROR
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2589 # recommend g_path_get_* over g_strdup(basename/dirname(...))
2590 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) {
2591 WARN
("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
2594 # recommend qemu_strto* over strto* for numeric conversions
2595 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2596 ERROR
("consider using qemu_$1 in preference to $1\n" . $herecurr);
2598 # recommend sigaction over signal for portability, when establishing a handler
2599 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2600 ERROR
("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2602 # check for module_init(), use category-specific init macros explicitly please
2603 if ($line =~ /^module_init\s*\(/) {
2604 ERROR
("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2606 # check for various ops structs, ensure they are const.
2607 my $struct_ops = qr{AIOCBInfo
|
2611 DisplayChangeListenerOps
|
2615 MemoryRegionIOMMUOps
|
2621 Spice
[A
-Z
][a
-zA
-Z0
-9]*Interface
|
2622 USBDesc
[A
-Z
][a
-zA
-Z0
-9]*|
2626 if ($line !~ /\bconst\b/ &&
2627 $line =~ /\b($struct_ops)\b.*=/) {
2628 ERROR
("initializer for struct $1 should normally be const\n" .
2632 # check for %L{u,d,i} in strings
2634 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2635 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2636 $string =~ s/%%/__/g;
2637 if ($string =~ /(?<!%)%L[udi]/) {
2638 ERROR
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2643 # QEMU specific tests
2644 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2645 ERROR
("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2648 # Qemu error function tests
2650 # Find newlines in error messages
2651 my $qemu_error_funcs = qr{error_setg
|
2654 error_setg_file_open
|
2666 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2667 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2670 # Continue checking for error messages that contains newlines. This
2671 # check handles cases where string literals are spread over multiple lines.
2673 # error_report("Error msg line #1"
2674 # "Error msg line #2\n");
2675 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2676 my $continued_str_literal = qr{\+\s*\".*\"};
2678 if ($rawline =~ /$quoted_newline_regex/) {
2679 # Backtrack to first line that does not contain only a quoted literal
2680 # and assume that it is the start of the statement.
2681 my $i = $linenr - 2;
2683 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2687 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2688 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2692 # check for non-portable libc calls that have portable alternatives in QEMU
2693 if ($line =~ /\bffs\(/) {
2694 ERROR
("use ctz32() instead of ffs()\n" . $herecurr);
2696 if ($line =~ /\bffsl\(/) {
2697 ERROR
("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2699 if ($line =~ /\bffsll\(/) {
2700 ERROR
("use ctz64() instead of ffsll()\n" . $herecurr);
2702 if ($line =~ /\bbzero\(/) {
2703 ERROR
("use memset() instead of bzero()\n" . $herecurr);
2705 my $non_exit_glib_asserts = qr{g_assert_cmpstr
|
2716 g_test_assert_expected_messages
|
2717 g_test_trap_assert_passed
|
2718 g_test_trap_assert_stdout
|
2719 g_test_trap_assert_stdout_unmatched
|
2720 g_test_trap_assert_stderr
|
2721 g_test_trap_assert_stderr_unmatched
}x
;
2722 if ($realfile !~ /^tests\// &&
2723 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2724 ERROR
("Use g_assert or g_assert_not_reached\n". $herecurr);
2728 # If we have no input at all, then there is nothing to report on
2729 # so just keep quiet.
2730 if ($#rawlines == -1) {
2734 # In mailback mode only produce a report in the negative, for
2735 # things that appear to be patches.
2736 if ($mailback && ($clean == 1 || !$is_patch)) {
2740 # This is not a patch, and we are are in 'no-patch' mode so
2742 if (!$chk_patch && !$is_patch) {
2747 ERROR
("Does not appear to be a unified-diff format patch\n");
2749 if ($is_patch && $chk_signoff && $signoff == 0) {
2750 ERROR
("Missing Signed-off-by: line(s)\n");
2753 print report_dump
();
2754 if ($summary && !($clean == 1 && $quiet == 1)) {
2755 print "$filename " if ($summary_file);
2756 print "total: $cnt_error errors, $cnt_warn warnings, " .
2757 "$cnt_lines lines checked\n";
2758 print "\n" if ($quiet == 0);
2762 # If there were whitespace errors which cleanpatch can fix
2763 # then suggest that.
2764 # if ($rpt_cleaners) {
2765 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2766 # print " scripts/cleanfile\n\n";
2770 if ($clean == 1 && $quiet == 0) {
2771 print "$vname has no obvious style problems and is ready for submission.\n"
2773 if ($clean == 0 && $quiet == 0) {
2774 print "$vname has style problems, please review. If any of these errors\n";
2775 print "are false positives report them to the maintainer, see\n";
2776 print "CHECKPATCH in MAINTAINERS.\n";
2779 return ($no_warnings ?
$clean : $cnt_error == 0);