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
16 use Getopt
::Long
qw(:config no_auto_abbrev);
21 my $chk_patch = undef;
22 my $chk_branch = undef;
41 $P [OPTION
]... [FILE
]...
42 $P [OPTION
]... [GIT
-REV
-LIST
]
48 --no-tree run without a kernel tree
49 --no-signoff
do not check
for 'Signed-off-by' line
50 --patch treat FILE as patchfile
51 --branch treat args as GIT revision list
52 --emacs emacs compile window format
53 --terse one line per report
54 -f
, --file treat FILE as regular source file
55 --strict fail
if only warnings are found
56 --root
=PATH PATH to the kernel tree root
57 --no-summary suppress the per
-file summary
58 --mailback only produce a report
in case of warnings
/errors
59 --summary
-file include the filename
in summary
60 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
61 'values', 'possible', 'type', and 'attr' (default
63 --test
-only
=WORD report only warnings
/errors containing WORD
65 -h
, --help
, --version display this help
and exit
67 When FILE is
- read standard input
.
74 'q|quiet+' => \
$quiet,
76 'signoff!' => \
$chk_signoff,
77 'patch!' => \
$chk_patch,
78 'branch!' => \
$chk_branch,
82 'strict!' => \
$no_warnings,
84 'summary!' => \
$summary,
85 'mailback!' => \
$mailback,
86 'summary-file!' => \
$summary_file,
89 'test-only=s' => \
$tst_only,
99 print "$P: no input files\n";
103 if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
104 $chk_branch = $ARGV[0] =~ /\.\./ ?
1 : 0;
105 $chk_patch = $chk_branch ?
0 :
106 $ARGV[0] =~ /\.patch$/ || $ARGV[0] eq "-" ?
1 : 0;
107 $file = $chk_branch || $chk_patch ?
0 : 1;
108 } elsif (!defined $chk_branch && !defined $chk_patch) {
110 $chk_branch = $chk_patch = 0;
112 $chk_branch = $ARGV[0] =~ /\.\./ ?
1 : 0;
113 $chk_patch = $chk_branch ?
0 : 1;
115 } elsif (!defined $chk_branch && !defined $file) {
117 $chk_branch = $file = 0;
119 $chk_branch = $ARGV[0] =~ /\.\./ ?
1 : 0;
120 $file = $chk_branch ?
0 : 1;
122 } elsif (!defined $chk_patch && !defined $file) {
124 $chk_patch = $file = 0;
126 $chk_patch = $ARGV[0] =~ /\.patch$/ || $ARGV[0] eq "-" ?
1 : 0;
127 $file = $chk_patch ?
0 : 1;
129 } elsif (!defined $chk_branch) {
130 $chk_branch = $chk_patch || $file ?
0 : 1;
131 } elsif (!defined $chk_patch) {
132 $chk_patch = $chk_branch || $file ?
0 : 1;
133 } elsif (!defined $file) {
134 $file = $chk_patch || $chk_branch ?
0 : 1;
137 if (($chk_patch && $chk_branch) ||
138 ($chk_patch && $file) ||
139 ($chk_branch && $file)) {
140 die "Only one of --file, --branch, --patch is permitted\n";
142 if (!$chk_patch && !$chk_branch && !$file) {
143 die "One of --file, --branch, --patch is required\n";
147 my $dbg_possible = 0;
151 my $dbg_adv_checking = 0;
153 for my $key (keys %debug) {
155 eval "\${dbg_$key} = '$debug{$key}';";
159 my $rpt_cleaners = 0;
168 if (!top_of_kernel_tree
($root)) {
169 die "$P: $root: --root does not point at a valid tree\n";
172 if (top_of_kernel_tree
('.')) {
174 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
175 top_of_kernel_tree
($1)) {
180 if (!defined $root) {
181 print "Must be run from the top-level dir. of a kernel tree\n";
186 my $emitted_corrupt = 0;
189 [A
-Za
-z_
][A
-Za
-z\d_
]*
190 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
192 our $Storage = qr{extern|static|asmlinkage};
197 # Notes to $Attribute:
202 QEMU_WARN_UNUSED_RESULT
|
209 our $Inline = qr{inline};
210 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
211 our $Lval = qr{$Ident(?:$Member)*};
213 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
214 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
215 our $Compare = qr{<=|>=|==|!=|<|>};
219 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
227 [\x09\x0A\x0D\x20-\x7E] # ASCII
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
237 # There are still some false positives, but this catches most
239 our $typeTypedefs = qr{(?x
:
240 [A
-Z
][A
-Z\d_
]*[a
-z
][A
-Za
-z\d_
]* # camelcase
241 | [A
-Z
][A
-Z\d_
]*AIOCB
# all uppercase
242 | [A
-Z
][A
-Z\d_
]*CPU
# all uppercase
243 | QEMUBH
# all uppercase
248 qr{(?:unsigned\s+)?char},
249 qr{(?:unsigned\s+)?short},
250 qr{(?:unsigned\s+)?int},
251 qr{(?:unsigned\s+)?long},
252 qr{(?:unsigned\s+)?long\s+int},
253 qr{(?:unsigned\s+)?long\s+long},
254 qr{(?:unsigned\s+)?long\s+long\s+int},
263 qr{${Ident}_handler
},
264 qr{${Ident}_handler_fn
},
265 qr{target_(?:u)?long},
269 # This can be modified by sub possible. Since it can be empty, be careful
270 # about regexes that always match, because they can cause infinite loops.
271 our @modifierList = (
275 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
276 if (@modifierList > 0) {
277 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
278 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
280 $Modifier = qr{(?:$Attribute|$Sparse)};
283 (?
:$Modifier\s
+|const\s
+)*
285 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)|
289 (?
:\s
+$Modifier|\s
+const
)*
293 (?
:[\s\
*]+\s
*const
|[\s\
*]+|(?
:\s
*\
[\s
*\
])+)?
294 (?
:\s
+$Inline|\s
+$Modifier)*
296 $Declare = qr{(?:$Storage\s+)?$Type};
300 $chk_signoff = 0 if ($file);
308 open($HASH, "-|", "git", "log", "--format=%H", $ARGV[0]) ||
309 die "$P: git log --format=%H $ARGV[0] failed - $!\n";
318 die "$P: no revisions returned for revlist '$chk_branch'\n"
321 for my $hash (@patches) {
323 open($FILE, '-|', "git", "show", $hash) ||
324 die "$P: git show $hash - $!\n";
331 if (!process
($hash)) {
338 for my $filename (@ARGV) {
341 open($FILE, '-|', "diff -u /dev/null $filename") ||
342 die "$P: $filename: diff failed - $!\n";
343 } elsif ($filename eq '-') {
344 open($FILE, '<&STDIN');
346 open($FILE, '<', "$filename") ||
347 die "$P: $filename: open failed - $!\n";
349 if ($filename eq '-') {
350 $vname = 'Your patch';
359 if (!process
($filename)) {
369 sub top_of_kernel_tree
{
373 "COPYING", "MAINTAINERS", "Makefile",
374 "README", "docs", "VERSION",
378 foreach my $check (@tree_check) {
379 if (! -e
$root . '/' . $check) {
391 for my $c (split(//, $str)) {
395 for (; ($n % 8) != 0; $n++) {
407 (my $res = shift) =~ tr/\t/ /c;
414 # Drop the diff line leader and expand tabs
416 $line = expand_tabs
($line);
418 # Pick the indent from the front of the line.
419 my ($white) = ($line =~ /^(\s*)/);
421 return (length($line), length($white));
424 my $sanitise_quote = '';
426 sub sanitise_line_reset
{
427 my ($in_comment) = @_;
430 $sanitise_quote = '*/';
432 $sanitise_quote = '';
445 # Always copy over the diff marker.
446 $res = substr($line, 0, 1);
448 for ($off = 1; $off < length($line); $off++) {
449 $c = substr($line, $off, 1);
451 # Comments we are wacking completely including the begin
452 # and end, all to $;.
453 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
454 $sanitise_quote = '*/';
456 substr($res, $off, 2, "$;$;");
460 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
461 $sanitise_quote = '';
462 substr($res, $off, 2, "$;$;");
466 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
467 $sanitise_quote = '//';
469 substr($res, $off, 2, $sanitise_quote);
474 # A \ in a string means ignore the next character.
475 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
477 substr($res, $off, 2, 'XX');
482 if ($c eq "'" || $c eq '"') {
483 if ($sanitise_quote eq '') {
484 $sanitise_quote = $c;
486 substr($res, $off, 1, $c);
488 } elsif ($sanitise_quote eq $c) {
489 $sanitise_quote = '';
493 #print "c<$c> SQ<$sanitise_quote>\n";
494 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
495 substr($res, $off, 1, $;);
496 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
497 substr($res, $off, 1, $;);
498 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
499 substr($res, $off, 1, 'X');
501 substr($res, $off, 1, $c);
505 if ($sanitise_quote eq '//') {
506 $sanitise_quote = '';
509 # The pathname on a #include may be surrounded by '<' and '>'.
510 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
511 my $clean = 'X' x
length($1);
512 $res =~ s@\
<.*\
>@
<$clean>@
;
514 # The whole of a #error is a string.
515 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
516 my $clean = 'X' x
length($1);
517 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
523 sub ctx_statement_block
{
524 my ($linenr, $remain, $off) = @_;
525 my $line = $linenr - 1;
542 @stack = (['', 0]) if ($#stack == -1);
544 #warn "CSB: blk<$blk> remain<$remain>\n";
545 # If we are about to drop off the end, pull in more
548 for (; $remain > 0; $line++) {
549 last if (!defined $lines[$line]);
550 next if ($lines[$line] =~ /^-/);
553 $blk .= $lines[$line] . "\n";
558 # Bail if there is no further context.
559 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
565 $c = substr($blk, $off, 1);
566 $remainder = substr($blk, $off);
568 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
570 # Handle nested #if/#else.
571 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
572 push(@stack, [ $type, $level ]);
573 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
574 ($type, $level) = @
{$stack[$#stack - 1]};
575 } elsif ($remainder =~ /^#\s*endif\b/) {
576 ($type, $level) = @
{pop(@stack)};
579 # Statement ends at the ';' or a close '}' at the
581 if ($level == 0 && $c eq ';') {
585 # An else is really a conditional as long as its not else if
586 if ($level == 0 && $coff_set == 0 &&
587 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
588 $remainder =~ /^(else)(?:\s|{)/ &&
589 $remainder !~ /^else\s+if\b/) {
590 $coff = $off + length($1) - 1;
592 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
593 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
596 if (($type eq '' || $type eq '(') && $c eq '(') {
600 if ($type eq '(' && $c eq ')') {
602 $type = ($level != 0)?
'(' : '';
604 if ($level == 0 && $coff < $soff) {
607 #warn "CSB: mark coff<$coff>\n";
610 if (($type eq '' || $type eq '{') && $c eq '{') {
614 if ($type eq '{' && $c eq '}') {
616 $type = ($level != 0)?
'{' : '';
619 if (substr($blk, $off + 1, 1) eq ';') {
627 # We are truly at the end, so shuffle to the next line.
634 my $statement = substr($blk, $soff, $off - $soff + 1);
635 my $condition = substr($blk, $soff, $coff - $soff + 1);
637 #warn "STATEMENT<$statement>\n";
638 #warn "CONDITION<$condition>\n";
640 #print "coff<$coff> soff<$off> loff<$loff>\n";
642 return ($statement, $condition,
643 $line, $remain + 1, $off - $loff + 1, $level);
646 sub statement_lines
{
649 # Strip the diff line prefixes and rip blank lines at start and end.
650 $stmt =~ s/(^|\n)./$1/g;
654 my @stmt_lines = ($stmt =~ /\n/g);
656 return $#stmt_lines + 2;
659 sub statement_rawlines
{
662 my @stmt_lines = ($stmt =~ /\n/g);
664 return $#stmt_lines + 2;
667 sub statement_block_size
{
670 $stmt =~ s/(^|\n)./$1/g;
676 my @stmt_lines = ($stmt =~ /\n/g);
677 my @stmt_statements = ($stmt =~ /;/g);
679 my $stmt_lines = $#stmt_lines + 2;
680 my $stmt_statements = $#stmt_statements + 1;
682 if ($stmt_lines > $stmt_statements) {
685 return $stmt_statements;
689 sub ctx_statement_full
{
690 my ($linenr, $remain, $off) = @_;
691 my ($statement, $condition, $level);
695 # Grab the first conditional/block pair.
696 ($statement, $condition, $linenr, $remain, $off, $level) =
697 ctx_statement_block
($linenr, $remain, $off);
698 #print "F: c<$condition> s<$statement> remain<$remain>\n";
699 push(@chunks, [ $condition, $statement ]);
700 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
701 return ($level, $linenr, @chunks);
704 # Pull in the following conditional/block pairs and see if they
705 # could continue the statement.
707 ($statement, $condition, $linenr, $remain, $off, $level) =
708 ctx_statement_block
($linenr, $remain, $off);
709 #print "C: c<$condition> s<$statement> remain<$remain>\n";
710 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
712 push(@chunks, [ $condition, $statement ]);
715 return ($level, $linenr, @chunks);
719 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
721 my $start = $linenr - 1;
728 my @stack = ($level);
729 for ($line = $start; $remain > 0; $line++) {
730 next if ($rawlines[$line] =~ /^-/);
733 $blk .= $rawlines[$line];
735 # Handle nested #if/#else.
736 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
737 push(@stack, $level);
738 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
739 $level = $stack[$#stack - 1];
740 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
741 $level = pop(@stack);
744 foreach my $c (split(//, $lines[$line])) {
745 ##print "C<$c>L<$level><$open$close>O<$off>\n";
751 if ($c eq $close && $level > 0) {
753 last if ($level == 0);
754 } elsif ($c eq $open) {
759 if (!$outer || $level <= 1) {
760 push(@res, $rawlines[$line]);
763 last if ($level == 0);
766 return ($level, @res);
768 sub ctx_block_outer
{
769 my ($linenr, $remain) = @_;
771 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
775 my ($linenr, $remain) = @_;
777 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
781 my ($linenr, $remain, $off) = @_;
783 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
786 sub ctx_block_level
{
787 my ($linenr, $remain) = @_;
789 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
791 sub ctx_statement_level
{
792 my ($linenr, $remain, $off) = @_;
794 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
797 sub ctx_locate_comment
{
798 my ($first_line, $end_line) = @_;
800 # Catch a comment on the end of the line itself.
801 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
802 return $current_comment if (defined $current_comment);
804 # Look through the context and try and figure out if there is a
807 $current_comment = '';
808 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
809 my $line = $rawlines[$linenr - 1];
811 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
814 if ($line =~ m@
/\
*@
) {
817 if (!$in_comment && $current_comment ne '') {
818 $current_comment = '';
820 $current_comment .= $line . "\n" if ($in_comment);
821 if ($line =~ m@\
*/@
) {
826 chomp($current_comment);
827 return($current_comment);
829 sub ctx_has_comment
{
830 my ($first_line, $end_line) = @_;
831 my $cmt = ctx_locate_comment
($first_line, $end_line);
833 ##print "LINE: $rawlines[$end_line - 1 ]\n";
834 ##print "CMMT: $cmt\n";
840 my ($linenr, $cnt) = @_;
842 my $offset = $linenr - 1;
847 $line = $rawlines[$offset++];
848 next if (defined($line) && $line =~ /^-/);
860 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
863 $coded = sprintf("^%c", unpack('C', $2) + 64);
872 my $av_preprocessor = 0;
878 $av_preprocessor = 0;
880 @av_paren_type = ('E');
881 $av_pend_colon = 'O';
884 sub annotate_values
{
885 my ($stream, $type) = @_;
888 my $var = '_' x
length($stream);
891 print "$stream\n" if ($dbg_values > 1);
893 while (length($cur)) {
894 @av_paren_type = ('E') if ($#av_paren_type < 0);
895 print " <" . join('', @av_paren_type) .
896 "> <$type> <$av_pending>" if ($dbg_values > 1);
897 if ($cur =~ /^(\s+)/o) {
898 print "WS($1)\n" if ($dbg_values > 1);
899 if ($1 =~ /\n/ && $av_preprocessor) {
900 $type = pop(@av_paren_type);
901 $av_preprocessor = 0;
904 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
905 print "CAST($1)\n" if ($dbg_values > 1);
906 push(@av_paren_type, $type);
909 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
910 print "DECLARE($1)\n" if ($dbg_values > 1);
913 } elsif ($cur =~ /^($Modifier)\s*/) {
914 print "MODIFIER($1)\n" if ($dbg_values > 1);
917 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
918 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
919 $av_preprocessor = 1;
920 push(@av_paren_type, $type);
926 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
927 print "UNDEF($1)\n" if ($dbg_values > 1);
928 $av_preprocessor = 1;
929 push(@av_paren_type, $type);
931 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
932 print "PRE_START($1)\n" if ($dbg_values > 1);
933 $av_preprocessor = 1;
935 push(@av_paren_type, $type);
936 push(@av_paren_type, $type);
939 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
940 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
941 $av_preprocessor = 1;
943 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
947 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
948 print "PRE_END($1)\n" if ($dbg_values > 1);
950 $av_preprocessor = 1;
952 # Assume all arms of the conditional end as this
953 # one does, and continue as if the #endif was not here.
955 push(@av_paren_type, $type);
958 } elsif ($cur =~ /^(\\\n)/o) {
959 print "PRECONT($1)\n" if ($dbg_values > 1);
961 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
962 print "ATTR($1)\n" if ($dbg_values > 1);
966 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
967 print "SIZEOF($1)\n" if ($dbg_values > 1);
973 } elsif ($cur =~ /^(if|while|for)\b/o) {
974 print "COND($1)\n" if ($dbg_values > 1);
978 } elsif ($cur =~/^(case)/o) {
979 print "CASE($1)\n" if ($dbg_values > 1);
980 $av_pend_colon = 'C';
983 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
984 print "KEYWORD($1)\n" if ($dbg_values > 1);
987 } elsif ($cur =~ /^(\()/o) {
988 print "PAREN('$1')\n" if ($dbg_values > 1);
989 push(@av_paren_type, $av_pending);
993 } elsif ($cur =~ /^(\))/o) {
994 my $new_type = pop(@av_paren_type);
995 if ($new_type ne '_') {
997 print "PAREN('$1') -> $type\n"
998 if ($dbg_values > 1);
1000 print "PAREN('$1')\n" if ($dbg_values > 1);
1003 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1004 print "FUNC($1)\n" if ($dbg_values > 1);
1008 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1009 if (defined $2 && $type eq 'C' || $type eq 'T') {
1010 $av_pend_colon = 'B';
1011 } elsif ($type eq 'E') {
1012 $av_pend_colon = 'L';
1014 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1017 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1018 print "IDENT($1)\n" if ($dbg_values > 1);
1021 } elsif ($cur =~ /^($Assignment)/o) {
1022 print "ASSIGN($1)\n" if ($dbg_values > 1);
1025 } elsif ($cur =~/^(;|{|})/) {
1026 print "END($1)\n" if ($dbg_values > 1);
1028 $av_pend_colon = 'O';
1030 } elsif ($cur =~/^(,)/) {
1031 print "COMMA($1)\n" if ($dbg_values > 1);
1034 } elsif ($cur =~ /^(\?)/o) {
1035 print "QUESTION($1)\n" if ($dbg_values > 1);
1038 } elsif ($cur =~ /^(:)/o) {
1039 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1041 substr($var, length($res), 1, $av_pend_colon);
1042 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1047 $av_pend_colon = 'O';
1049 } elsif ($cur =~ /^(\[)/o) {
1050 print "CLOSE($1)\n" if ($dbg_values > 1);
1053 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1056 print "OPV($1)\n" if ($dbg_values > 1);
1063 substr($var, length($res), 1, $variant);
1066 } elsif ($cur =~ /^($Operators)/o) {
1067 print "OP($1)\n" if ($dbg_values > 1);
1068 if ($1 ne '++' && $1 ne '--') {
1072 } elsif ($cur =~ /(^.)/o) {
1073 print "C($1)\n" if ($dbg_values > 1);
1076 $cur = substr($cur, length($1));
1077 $res .= $type x
length($1);
1081 return ($res, $var);
1085 my ($possible, $line) = @_;
1086 my $notPermitted = qr{(?
:
1103 ^(?
:typedef
|struct
|enum
)\b
1105 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1106 if ($possible !~ $notPermitted) {
1107 # Check for modifiers.
1108 $possible =~ s/\s*$Storage\s*//g;
1109 $possible =~ s/\s*$Sparse\s*//g;
1110 if ($possible =~ /^\s*$/) {
1112 } elsif ($possible =~ /\s/) {
1113 $possible =~ s/\s*$Type\s*//g;
1114 for my $modifier (split(' ', $possible)) {
1115 if ($modifier !~ $notPermitted) {
1116 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1117 push(@modifierList, $modifier);
1122 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1123 push(@typeList, $possible);
1127 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1134 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1137 my $line = $prefix . $_[0];
1139 $line = (split('\n', $line))[0] . "\n" if ($terse);
1141 push(our @report, $line);
1149 if (report
("ERROR: $_[0]\n")) {
1155 if (report
("WARNING: $_[0]\n")) {
1162 my $filename = shift;
1168 my $stashrawline="";
1185 # Trace the real file/line as we go.
1191 my $comment_edge = 0;
1195 my $prev_values = 'E';
1198 my %suppress_ifbraces;
1199 my %suppress_whiletrailers;
1200 my %suppress_export;
1202 # Pre-scan the patch sanitizing the lines.
1204 sanitise_line_reset
();
1206 foreach my $rawline (@rawlines) {
1210 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1219 # Guestimate if this is a continuing comment. Run
1220 # the context looking for a comment "edge". If this
1221 # edge is a close comment then we must be in a comment
1225 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1226 next if (defined $rawlines[$ln - 1] &&
1227 $rawlines[$ln - 1] =~ /^-/);
1229 #print "RAW<$rawlines[$ln - 1]>\n";
1230 last if (!defined $rawlines[$ln - 1]);
1231 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1232 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1237 if (defined $edge && $edge eq '*/') {
1241 # Guestimate if this is a continuing comment. If this
1242 # is the start of a diff block and this line starts
1243 # ' *' then it is very likely a comment.
1244 if (!defined $edge &&
1245 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1250 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1251 sanitise_line_reset
($in_comment);
1253 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1254 # Standardise the strings and chars within the input to
1255 # simplify matching -- only bother with positive lines.
1256 $line = sanitise_line
($rawline);
1258 push(@lines, $line);
1261 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1266 #print "==>$rawline\n";
1267 #print "-->$line\n";
1274 foreach my $line (@lines) {
1277 my $rawline = $rawlines[$linenr - 1];
1279 #extract the line range in the file after the patch is applied
1280 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1282 $first_line = $linenr + 1;
1292 %suppress_ifbraces = ();
1293 %suppress_whiletrailers = ();
1294 %suppress_export = ();
1297 # track the line number as we move through the hunk, note that
1298 # new versions of GNU diff omit the leading space on completely
1299 # blank context lines so we need to count that too.
1300 } elsif ($line =~ /^( |\+|$)/) {
1302 $realcnt-- if ($realcnt != 0);
1304 # Measure the line length and indent.
1305 ($length, $indent) = line_stats
($rawline);
1307 # Track the previous line.
1308 ($prevline, $stashline) = ($stashline, $line);
1309 ($previndent, $stashindent) = ($stashindent, $indent);
1310 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1312 #warn "line<$line>\n";
1314 } elsif ($realcnt == 1) {
1318 my $hunk_line = ($realcnt != 0);
1320 #make up the handle for any error we report on this line
1321 $prefix = "$filename:$realline: " if ($emacs && $file);
1322 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1324 $here = "#$linenr: " if (!$file);
1325 $here = "#$realline: " if ($file);
1327 # extract the filename as it passes
1328 if ($line =~ /^diff --git.*?(\S+)$/) {
1330 $realfile =~ s@
^([^/]*)/@@
;
1332 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1334 $realfile =~ s@
^([^/]*)/@@
;
1337 if (!$file && $tree && $p1_prefix ne '' &&
1338 -e
"$root/$p1_prefix") {
1339 WARN
("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1345 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1347 my $hereline = "$here\n$rawline\n";
1348 my $herecurr = "$here\n$rawline\n";
1349 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1351 $cnt_lines++ if ($realcnt != 0);
1353 # Check for incorrect file permissions
1354 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1355 my $permhere = $here . "FILE: $realfile\n";
1356 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1357 ERROR
("do not set execute permissions for source files\n" . $permhere);
1361 # Accept git diff extended headers as valid patches
1362 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\
.\
-]+\s
*$/) {
1366 #check the patch for a signoff:
1367 if ($line =~ /^\s*signed-off-by:/i) {
1368 # This is a signoff, if ugly, so do not double report.
1370 if (!($line =~ /^\s*Signed-off-by:/)) {
1371 ERROR
("The correct form is \"Signed-off-by\"\n" .
1374 if ($line =~ /^\s*signed-off-by:\S/i) {
1375 ERROR
("space required after Signed-off-by:\n" .
1380 # Check for wrappage within a valid hunk of the file
1381 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1382 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1383 $herecurr) if (!$emitted_corrupt++);
1386 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1387 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1388 $rawline !~ m/^$UTF8*$/) {
1389 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1391 my $blank = copy_spacing
($rawline);
1392 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1393 my $hereptr = "$hereline$ptr\n";
1395 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1398 # ignore non-hunk lines and lines being removed
1399 next if (!$hunk_line || $line =~ /^-/);
1401 # ignore files that are being periodically imported from Linux
1402 next if ($realfile =~ /^(linux-headers|include\/standard
-headers
)\
//);
1404 #trailing whitespace
1405 if ($line =~ /^\+.*\015/) {
1406 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1407 ERROR
("DOS line endings\n" . $herevet);
1409 } elsif ($realfile =~ /^docs\/.+\
.txt
/ ||
1410 $realfile =~ /^docs\/.+\
.md
/) {
1411 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1412 # TODO: properly check we're in a code block
1413 # (surrounding text is 4-column aligned)
1414 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1415 ERROR
("code blocks in documentation should have " .
1416 "empty lines with exactly 4 columns of " .
1417 "whitespace\n" . $herevet);
1419 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1420 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1421 ERROR
("trailing whitespace\n" . $herevet);
1425 # checks for trace-events files
1426 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1427 if ($rawline =~ /%[-+ 0]*#/) {
1428 ERROR
("Don't use '#' flag of printf format ('%#') in " .
1429 "trace-events, use '0x' prefix instead\n" . $herecurr);
1432 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1434 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
1435 my $tmpline = $rawline;
1436 $tmpline =~ s/($hex[.:\/ ])+$hex//g
;
1438 if ($tmpline =~ /(?<!0x)$hex/) {
1439 ERROR
("Hex numbers must be prefixed with '0x'\n" .
1445 # check we are in a valid source file if not then ignore this hunk
1446 next if ($realfile !~ /\.(h|c|cpp|s|S|pl|py|sh)$/);
1449 if ($line =~ /^\+/ &&
1450 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1454 ERROR
("line over 90 characters\n" . $herecurr);
1456 WARN
("line over 80 characters\n" . $herecurr);
1460 # check for spaces before a quoted newline
1461 if ($rawline =~ /^.*\".*\s\\n/) {
1462 ERROR
("unnecessary whitespace before a quoted newline\n" . $herecurr);
1465 # check for adding lines without a newline.
1466 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1467 ERROR
("adding a line without newline at end of file\n" . $herecurr);
1470 # check for RCS/CVS revision markers
1471 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1472 ERROR
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1475 # tabs are only allowed in assembly source code, and in
1476 # some scripts we imported from other projects.
1477 next if ($realfile =~ /\.(s|S)$/);
1478 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1480 if ($rawline =~ /^\+.*\t/) {
1481 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1482 ERROR
("code indent should never use tabs\n" . $herevet);
1486 # check we are in a valid C source file if not then ignore this hunk
1487 next if ($realfile !~ /\.(h|c|cpp)$/);
1489 # Check for potential 'bare' types
1490 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1492 if ($realcnt && $line =~ /.\s*\S/) {
1493 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1494 ctx_statement_block
($linenr, $realcnt, 0);
1495 $stat =~ s/\n./\n /g;
1496 $cond =~ s/\n./\n /g;
1498 # Find the real next line.
1499 $realline_next = $line_nr_next;
1500 if (defined $realline_next &&
1501 (!defined $lines[$realline_next - 1] ||
1502 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1509 # Ignore goto labels.
1510 if ($s =~ /$Ident:\*$/s) {
1512 # Ignore functions being called
1513 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1515 } elsif ($s =~ /^.\s*else\b/s) {
1517 # declarations always start with types
1518 } 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) {
1521 possible
($type, "A:" . $s);
1523 # definitions in global scope can only start with types
1524 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1525 possible
($1, "B:" . $s);
1528 # any (foo ... *) is a pointer cast, and foo is a type
1529 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1530 possible
($1, "C:" . $s);
1533 # Check for any sort of function declaration.
1534 # int foo(something bar, other baz);
1535 # void (*store_gdt)(x86_descr_ptr *);
1536 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1537 my ($name_len) = length($1);
1540 substr($ctx, 0, $name_len + 1, '');
1541 $ctx =~ s/\)[^\)]*$//;
1543 for my $arg (split(/\s*,\s*/, $ctx)) {
1544 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1546 possible
($1, "D:" . $s);
1554 # Checks which may be anchored in the context.
1557 # Check for switch () and associated case and default
1558 # statements should be at the same indent.
1559 if ($line=~/\bswitch\s*\(.*\)/) {
1562 my @ctx = ctx_block_outer
($linenr, $realcnt);
1564 for my $ctx (@ctx) {
1565 my ($clen, $cindent) = line_stats
($ctx);
1566 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1567 $indent != $cindent) {
1568 $err .= "$sep$ctx\n";
1575 ERROR
("switch and case should be at the same indent\n$hereline$err");
1579 # if/while/etc brace do not go on next line, unless defining a do while loop,
1580 # or if that brace on the next line is for something else
1581 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1582 my $pre_ctx = "$1$2";
1584 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1585 my $ctx_cnt = $realcnt - $#ctx - 1;
1586 my $ctx = join("\n", @ctx);
1588 my $ctx_ln = $linenr;
1589 my $ctx_skip = $realcnt;
1591 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1592 defined $lines[$ctx_ln - 1] &&
1593 $lines[$ctx_ln - 1] =~ /^-/)) {
1594 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1595 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1599 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1600 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1602 # The length of the "previous line" is checked against 80 because it
1603 # includes the + at the beginning of the line (if the actual line has
1604 # 79 or 80 characters, it is no longer possible to add a space and an
1605 # opening brace there)
1606 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1607 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1608 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1609 ERROR
("that open brace { should be on the previous line\n" .
1610 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1612 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1613 $ctx =~ /\)\s*\;\s*$/ &&
1614 defined $lines[$ctx_ln - 1])
1616 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1617 if ($nindent > $indent) {
1618 ERROR
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1619 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1624 # Check relative indent for conditionals and blocks.
1625 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1626 my ($s, $c) = ($stat, $cond);
1628 substr($s, 0, length($c), '');
1630 # Make sure we remove the line prefixes as we have
1631 # none on the first line, and are going to readd them
1635 # Find out how long the conditional actually is.
1636 my @newlines = ($c =~ /\n/gs);
1637 my $cond_lines = 1 + $#newlines;
1639 # We want to check the first line inside the block
1640 # starting at the end of the conditional, so remove:
1641 # 1) any blank line termination
1642 # 2) any opening brace { on end of the line
1644 my $continuation = 0;
1646 $s =~ s/^.*\bdo\b//;
1648 if ($s =~ s/^\s*\\//) {
1651 if ($s =~ s/^\s*?\n//) {
1656 # Also ignore a loop construct at the end of a
1657 # preprocessor statement.
1658 if (($prevline =~ /^.\s*#\s*define\s/ ||
1659 $prevline =~ /\\\s*$/) && $continuation == 0) {
1665 while ($cond_ptr != $cond_lines) {
1666 $cond_ptr = $cond_lines;
1668 # If we see an #else/#elif then the code
1670 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1675 # 1) blank lines, they should be at 0,
1676 # 2) preprocessor lines, and
1678 if ($continuation ||
1680 $s =~ /^\s*#\s*?/ ||
1681 $s =~ /^\s*$Ident\s*:/) {
1682 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
1683 if ($s =~ s/^.*?\n//) {
1689 my (undef, $sindent) = line_stats
("+" . $s);
1690 my $stat_real = raw_line
($linenr, $cond_lines);
1692 # Check if either of these lines are modified, else
1693 # this is not this patch's fault.
1694 if (!defined($stat_real) ||
1695 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1698 if (defined($stat_real) && $cond_lines > 1) {
1699 $stat_real = "[...]\n$stat_real";
1702 #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";
1704 if ($check && (($sindent % 4) != 0 ||
1705 ($sindent <= $indent && $s ne ''))) {
1706 ERROR
("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1710 # Track the 'values' across context and added lines.
1711 my $opline = $line; $opline =~ s/^./ /;
1712 my ($curr_values, $curr_vars) =
1713 annotate_values
($opline . "\n", $prev_values);
1714 $curr_values = $prev_values . $curr_values;
1716 my $outline = $opline; $outline =~ s/\t/ /g;
1717 print "$linenr > .$outline\n";
1718 print "$linenr > $curr_values\n";
1719 print "$linenr > $curr_vars\n";
1721 $prev_values = substr($curr_values, -1);
1723 #ignore lines not being added
1724 if ($line=~/^[^\+]/) {next;}
1726 # TEST: allow direct testing of the type matcher.
1728 if ($line =~ /^.\s*$Declare\s*$/) {
1729 ERROR
("TEST: is type\n" . $herecurr);
1730 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1731 ERROR
("TEST: is not type ($1 is)\n". $herecurr);
1735 # TEST: allow direct testing of the attribute matcher.
1737 if ($line =~ /^.\s*$Modifier\s*$/) {
1738 ERROR
("TEST: is attr\n" . $herecurr);
1739 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1740 ERROR
("TEST: is not attr ($1 is)\n". $herecurr);
1745 # check for initialisation to aggregates open brace on the next line
1746 if ($line =~ /^.\s*\{/ &&
1747 $prevline =~ /(?:^|[^=])=\s*$/) {
1748 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1752 # Checks which are anchored on the added line.
1755 # check for malformed paths in #include statements (uses RAW line)
1756 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1758 if ($path =~ m{//}) {
1759 ERROR
("malformed #include filename\n" .
1764 # no C99 // comments
1765 if ($line =~ m{//}) {
1766 ERROR
("do not use C99 // comments\n" . $herecurr);
1768 # Remove C99 comments.
1770 $opline =~ s@
//.*@@
;
1772 # check for global initialisers.
1773 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1774 ERROR
("do not initialise globals to 0 or NULL\n" .
1777 # check for static initialisers.
1778 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1779 ERROR
("do not initialise statics to 0 or NULL\n" .
1783 # * goes on variable not on type
1785 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1786 my ($from, $to) = ($1, $1);
1788 # Should start with a space.
1789 $to =~ s/^(\S)/ $1/;
1790 # Should not end with a space.
1792 # '*'s should not have spaces between.
1793 while ($to =~ s/\*\s+\*/\*\*/) {
1796 #print "from<$from> to<$to>\n";
1798 ERROR
("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1800 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1801 my ($from, $to, $ident) = ($1, $1, $2);
1803 # Should start with a space.
1804 $to =~ s/^(\S)/ $1/;
1805 # Should not end with a space.
1807 # '*'s should not have spaces between.
1808 while ($to =~ s/\*\s+\*/\*\*/) {
1810 # Modifiers should have spaces.
1811 $to =~ s/(\b$Modifier$)/$1 /;
1813 #print "from<$from> to<$to> ident<$ident>\n";
1814 if ($from ne $to && $ident !~ /^$Modifier$/) {
1815 ERROR
("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1819 # function brace can't be on same line, except for #defines of do while,
1820 # or if closed on same line
1821 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1822 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1823 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1826 # open braces for enum, union and struct go on the same line.
1827 if ($line =~ /^.\s*\{/ &&
1828 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1829 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1832 # missing space after union, struct or enum definition
1833 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1834 ERROR
("missing space after $1 definition\n" . $herecurr);
1837 # check for spacing round square brackets; allowed:
1838 # 1. with a type on the left -- int [] a;
1839 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1840 # 3. inside a curly brace -- = { [0...10] = 5 }
1841 # 4. after a comma -- [1] = 5, [2] = 6
1842 # 5. in a macro definition -- #define abc(x) [x] = y
1843 while ($line =~ /(.*?\s)\[/g) {
1844 my ($where, $prefix) = ($-[1], $1);
1845 if ($prefix !~ /$Type\s+$/ &&
1846 ($where != 0 || $prefix !~ /^.\s+$/) &&
1847 $prefix !~ /{\s+$/ &&
1848 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1849 $prefix !~ /,\s+$/) {
1850 ERROR
("space prohibited before open square bracket '['\n" . $herecurr);
1854 # check for spaces between functions and their parentheses.
1855 while ($line =~ /($Ident)\s+\(/g) {
1857 my $ctx_before = substr($line, 0, $-[1]);
1858 my $ctx = "$ctx_before$name";
1860 # Ignore those directives where spaces _are_ permitted.
1862 if|for|while|switch
|return|case
|
1863 volatile
|__volatile__
|coroutine_fn
|
1864 __attribute__
|format
|__extension__
|
1868 # Ignore 'catch (...)' in C++
1869 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1871 # cpp #define statements have non-optional spaces, ie
1872 # if there is a space between the name and the open
1873 # parenthesis it is simply not a parameter group.
1874 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1876 # cpp #elif statement condition may start with a (
1877 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1879 # If this whole things ends with a type its most
1880 # likely a typedef for a function.
1881 } elsif ($ctx =~ /$Type$/) {
1884 ERROR
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1887 # Check operator spacing.
1888 if (!($line=~/\#\s*include/)) {
1890 <<=|>>=|<=|>=|==|!=|
1891 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1892 =>|->|<<|>>|<|>|=|!|~|
1893 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
1896 my @elements = split(/($ops|;)/, $opline);
1899 my $blank = copy_spacing
($opline);
1901 for (my $n = 0; $n < $#elements; $n += 2) {
1902 $off += length($elements[$n]);
1904 # Pick up the preceding and succeeding characters.
1905 my $ca = substr($opline, 0, $off);
1907 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1908 $cc = substr($opline, $off + length($elements[$n + 1]));
1910 my $cb = "$ca$;$cc";
1913 $a = 'V' if ($elements[$n] ne '');
1914 $a = 'W' if ($elements[$n] =~ /\s$/);
1915 $a = 'C' if ($elements[$n] =~ /$;$/);
1916 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1917 $a = 'O' if ($elements[$n] eq '');
1918 $a = 'E' if ($ca =~ /^\s*$/);
1920 my $op = $elements[$n + 1];
1923 if (defined $elements[$n + 2]) {
1924 $c = 'V' if ($elements[$n + 2] ne '');
1925 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1926 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1927 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1928 $c = 'O' if ($elements[$n + 2] eq '');
1929 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1934 my $ctx = "${a}x${c}";
1936 my $at = "(ctx:$ctx)";
1938 my $ptr = substr($blank, 0, $off) . "^";
1939 my $hereptr = "$hereline$ptr\n";
1941 # Pull out the value of this operator.
1942 my $op_type = substr($curr_values, $off + 1, 1);
1944 # Get the full operator variant.
1945 my $opv = $op . substr($curr_vars, $off, 1);
1947 # Ignore operators passed as parameters.
1948 if ($op_type ne 'V' &&
1949 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1952 # } elsif ($op =~ /^$;+$/) {
1954 # ; should have either the end of line or a space or \ after it
1955 } elsif ($op eq ';') {
1956 if ($ctx !~ /.x[WEBC]/ &&
1957 $cc !~ /^\\/ && $cc !~ /^;/) {
1958 ERROR
("space required after that '$op' $at\n" . $hereptr);
1962 } elsif ($op eq '//') {
1964 # Ignore : used in class declaration in C++
1965 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
1966 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
1970 # : when part of a bitfield
1971 } elsif ($op eq '->' || $opv eq ':B') {
1972 if ($ctx =~ /Wx.|.xW/) {
1973 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
1976 # , must have a space on the right.
1977 # not required when having a single },{ on one line
1978 } elsif ($op eq ',') {
1979 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
1980 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
1981 ERROR
("space required after that '$op' $at\n" . $hereptr);
1984 # '*' as part of a type definition -- reported already.
1985 } elsif ($opv eq '*_') {
1986 #warn "'*' is part of type\n";
1988 # unary operators should have a space before and
1989 # none after. May be left adjacent to another
1990 # unary operator, or a cast
1991 } elsif ($op eq '!' || $op eq '~' ||
1992 $opv eq '*U' || $opv eq '-U' ||
1993 $opv eq '&U' || $opv eq '&&U') {
1994 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
1995 # '~' used as a name of Destructor
1997 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1998 ERROR
("space required before that '$op' $at\n" . $hereptr);
2000 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2001 # A unary '*' may be const
2003 } elsif ($ctx =~ /.xW/) {
2004 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2007 # unary ++ and unary -- are allowed no space on one side.
2008 } elsif ($op eq '++' or $op eq '--') {
2009 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2010 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
2012 if ($ctx =~ /Wx[BE]/ ||
2013 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2014 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2016 if ($ctx =~ /ExW/) {
2017 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2020 # A colon needs no spaces before when it is
2021 # terminating a case value or a label.
2022 } elsif ($opv eq ':C' || $opv eq ':L') {
2023 if ($ctx =~ /Wx./) {
2024 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2027 # All the others need spaces both sides.
2028 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2031 if ($realfile =~ /\.cpp|\.h$/) {
2032 # Ignore template arguments <...> in C++
2033 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2043 # Ignore email addresses <foo@bar>
2045 $cc =~ /^\S+\@\S+>/) ||
2047 $ca =~ /<\S+\@\S+$/))
2053 if (($opv eq ':O' && $ca =~ /\?$/) ||
2054 ($op eq '?' && $cc =~ /^:/)) {
2059 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
2062 $off += length($elements[$n + 1]);
2066 #need space before brace following if, while, etc
2067 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2069 ERROR
("space required before the open brace '{'\n" . $herecurr);
2072 # closing brace should have a space following it when it has anything
2074 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2075 ERROR
("space required after that close brace '}'\n" . $herecurr);
2078 # check spacing on square brackets
2079 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2080 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
2082 if ($line =~ /\s\]/) {
2083 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
2086 # check spacing on parentheses
2087 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2088 $line !~ /for\s*\(\s+;/) {
2089 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
2091 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2092 $line !~ /for\s*\(.*;\s+\)/ &&
2093 $line !~ /:\s+\)/) {
2094 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
2097 # Return is not a function.
2098 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2102 # Flatten any parentheses
2103 $value =~ s/\(/ \(/g;
2104 $value =~ s/\)/\) /g;
2105 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2106 $value !~ /(?
:$Ident|-?
$Constant)\s
*
2108 (?
:$Ident|-?
$Constant)/x
&&
2109 $value =~ s/\([^\(\)]*\)/1/) {
2111 #print "value<$value>\n";
2112 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2113 ERROR
("return is not a function, parentheses are not required\n" . $herecurr);
2115 } elsif ($spacing !~ /\s+/) {
2116 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2119 # Return of what appears to be an errno should normally be -'ve
2120 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2122 if ($name ne 'EOF' && $name ne 'ERROR') {
2123 ERROR
("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2127 # Need a space before open parenthesis after if, while etc
2128 if ($line=~/\b(if|while|for|switch)\(/) {
2129 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2132 # Check for illegal assignment in if conditional -- and check for trailing
2133 # statements after the conditional.
2134 if ($line =~ /do\s*(?!{)/) {
2135 my ($stat_next) = ctx_statement_block
($line_nr_next,
2136 $remain_next, $off_next);
2137 $stat_next =~ s/\n./\n /g;
2138 ##print "stat<$stat> stat_next<$stat_next>\n";
2140 if ($stat_next =~ /^\s*while\b/) {
2141 # If the statement carries leading newlines,
2142 # then count those as offsets.
2144 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2146 statement_rawlines
($whitespace) - 1;
2148 $suppress_whiletrailers{$line_nr_next +
2152 if (!defined $suppress_whiletrailers{$linenr} &&
2153 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2154 my ($s, $c) = ($stat, $cond);
2156 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2157 ERROR
("do not use assignment in if condition\n" . $herecurr);
2160 # Find out what is on the end of the line after the
2162 substr($s, 0, length($c), '');
2164 $s =~ s/$;//g; # Remove any comments
2165 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2166 $c !~ /}\s*while\s*/)
2168 # Find out how long the conditional actually is.
2169 my @newlines = ($c =~ /\n/gs);
2170 my $cond_lines = 1 + $#newlines;
2173 $stat_real = raw_line
($linenr, $cond_lines)
2174 . "\n" if ($cond_lines);
2175 if (defined($stat_real) && $cond_lines > 1) {
2176 $stat_real = "[...]\n$stat_real";
2179 ERROR
("trailing statements should be on next line\n" . $herecurr . $stat_real);
2183 # Check for bitwise tests written as boolean
2195 ERROR
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2198 # if and else should not have general statements after it
2199 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2201 $s =~ s/$;//g; # Remove any comments
2202 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2203 ERROR
("trailing statements should be on next line\n" . $herecurr);
2206 # if should not continue a brace
2207 if ($line =~ /}\s*if\b/) {
2208 ERROR
("trailing statements should be on next line\n" .
2211 # case and default should not have general statements after them
2212 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2214 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2218 ERROR
("trailing statements should be on next line\n" . $herecurr);
2221 # Check for }<nl>else {, these must be at the same
2222 # indent level to be relevant to each other.
2223 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2224 $previndent == $indent) {
2225 ERROR
("else should follow close brace '}'\n" . $hereprev);
2228 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2229 $previndent == $indent) {
2230 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2232 # Find out what is on the end of the line after the
2234 substr($s, 0, length($c), '');
2237 if ($s =~ /^\s*;/) {
2238 ERROR
("while should follow close brace '}'\n" . $hereprev);
2242 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2243 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2244 # print "No studly caps, use _\n";
2245 # print "$herecurr";
2249 #no spaces allowed after \ in define
2250 if ($line=~/\#\s*define.*\\\s$/) {
2251 ERROR
("Whitespace after \\ makes next lines useless\n" . $herecurr);
2254 # multi-statement macros should be enclosed in a do while loop, grab the
2255 # first statement and ensure its the whole macro if its not enclosed
2256 # in a known good container
2257 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2258 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2261 my ($off, $dstat, $dcond, $rest);
2264 my $args = defined($1);
2266 # Find the end of the macro and limit our statement
2268 while ($cnt > 0 && defined $lines[$ln - 1] &&
2269 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2271 $ctx .= $rawlines[$ln - 1] . "\n";
2272 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2275 $ctx .= $rawlines[$ln - 1];
2277 ($dstat, $dcond, $ln, $cnt, $off) =
2278 ctx_statement_block
($linenr, $ln - $linenr + 1, 0);
2279 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2280 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2282 # Extract the remainder of the define (if any) and
2283 # rip off surrounding spaces, and trailing \'s.
2285 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2286 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2287 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2288 $rest .= substr($lines[$ln - 1], $off) . "\n";
2294 $rest =~ s/\\\n.//g;
2298 # Clean up the original statement.
2300 substr($dstat, 0, length($dcond), '');
2302 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2305 $dstat =~ s/\\\n.//g;
2306 $dstat =~ s/^\s*//s;
2307 $dstat =~ s/\s*$//s;
2309 # Flatten any parentheses and braces
2310 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2311 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2312 $dstat =~ s/\[[^\{\}]*\]/1/)
2316 my $exceptions = qr{
2328 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2329 if ($rest ne '' && $rest ne ',') {
2330 if ($rest !~ /while\s*\(/ &&
2331 $dstat !~ /$exceptions/)
2333 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2336 } elsif ($ctx !~ /;/) {
2338 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2339 $dstat !~ /$exceptions/ &&
2340 $dstat !~ /^\.$Ident\s*=/ &&
2341 $dstat =~ /$Operators/)
2343 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2348 # check for missing bracing round if etc
2349 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2350 my ($level, $endln, @chunks) =
2351 ctx_statement_full
($linenr, $realcnt, 1);
2353 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2354 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2357 if ($#chunks >= 0 && $level == 0) {
2360 my $herectx = $here . "\n";
2361 my $ln = $linenr - 1;
2362 for my $chunk (@chunks) {
2363 my ($cond, $block) = @
{$chunk};
2365 # If the condition carries leading newlines, then count those as offsets.
2366 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2367 my $offset = statement_rawlines
($whitespace) - 1;
2369 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2371 # We have looked at and allowed this specific line.
2372 $suppress_ifbraces{$ln + $offset} = 1;
2374 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2375 $ln += statement_rawlines
($block) - 1;
2377 substr($block, 0, length($cond), '');
2379 my $spaced_block = $block;
2380 $spaced_block =~ s/\n\+/ /g;
2382 $seen++ if ($spaced_block =~ /^\s*\{/);
2384 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2386 if (statement_lines
($cond) > 1) {
2387 print "APW: ALLOWED: cond<$cond>\n"
2391 if ($block =~/\b(?:if|for|while)\b/) {
2392 print "APW: ALLOWED: block<$block>\n"
2396 if (statement_block_size
($block) > 1) {
2397 print "APW: ALLOWED: lines block<$block>\n"
2402 if ($seen != ($#chunks + 1)) {
2403 ERROR
("braces {} are necessary for all arms of this statement\n" . $herectx);
2407 if (!defined $suppress_ifbraces{$linenr - 1} &&
2408 $line =~ /\b(if|while|for|else)\b/ &&
2409 $line !~ /\#\s*if/ &&
2410 $line !~ /\#\s*else/) {
2413 # Check the pre-context.
2414 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2417 if ($line !~ /else/) {
2418 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2424 my ($level, $endln, @chunks) =
2425 ctx_statement_full
($linenr, $realcnt, $-[0]);
2427 # Check the condition.
2428 my ($cond, $block) = @
{$chunks[0]};
2429 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2430 if $dbg_adv_checking;
2431 if (defined $cond) {
2432 substr($block, 0, length($cond), '');
2434 if (statement_lines
($cond) > 1) {
2435 print "APW: ALLOWED: cond<$cond>\n"
2439 if ($block =~/\b(?:if|for|while)\b/) {
2440 print "APW: ALLOWED: block<$block>\n"
2444 if (statement_block_size
($block) > 1) {
2445 print "APW: ALLOWED: lines block<$block>\n"
2449 # Check the post-context.
2450 if (defined $chunks[1]) {
2451 my ($cond, $block) = @
{$chunks[1]};
2452 if (defined $cond) {
2453 substr($block, 0, length($cond), '');
2455 if ($block =~ /^\s*\{/) {
2456 print "APW: ALLOWED: chunk-1 block<$block>\n"
2461 print "DCS: level=$level block<$block> allowed=$allowed\n"
2463 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2464 my $herectx = $here . "\n";;
2465 my $cnt = statement_rawlines
($block);
2467 for (my $n = 0; $n < $cnt; $n++) {
2468 $herectx .= raw_line
($linenr, $n) . "\n";;
2471 ERROR
("braces {} are necessary even for single statement blocks\n" . $herectx);
2475 # no volatiles please
2476 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2477 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2478 ERROR
("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2482 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2483 ERROR
("if this code is redundant consider removing it\n" .
2487 # check for needless g_free() checks
2488 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2490 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2491 ERROR
("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2495 # warn about #ifdefs in C files
2496 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2497 # print "#ifdef in C files should be avoided\n";
2498 # print "$herecurr";
2502 # warn about spacing in #ifdefs
2503 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2504 ERROR
("exactly one space required after that #$1\n" . $herecurr);
2506 # check for memory barriers without a comment.
2507 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2508 if (!ctx_has_comment
($first_line, $linenr)) {
2509 ERROR
("memory barrier without comment\n" . $herecurr);
2512 # check of hardware specific defines
2513 # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2514 # where they might be necessary.
2515 if ($line =~ m@
^.\s
*\#\s
*if.*\b__@
) {
2516 WARN
("architecture specific defines should be avoided\n" . $herecurr);
2519 # Check that the storage class is at the beginning of a declaration
2520 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2521 ERROR
("storage class should be at the beginning of the declaration\n" . $herecurr)
2524 # check the location of the inline attribute, that it is between
2525 # storage class and type.
2526 if ($line =~ /\b$Type\s+$Inline\b/ ||
2527 $line =~ /\b$Inline\s+$Storage\b/) {
2528 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
2531 # check for sizeof(&)
2532 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2533 ERROR
("sizeof(& should be avoided\n" . $herecurr);
2536 # check for new externs in .c files.
2537 if ($realfile =~ /\.c$/ && defined $stat &&
2538 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2540 my $function_name = $1;
2541 my $paren_space = $2;
2544 if (defined $cond) {
2545 substr($s, 0, length($cond), '');
2547 if ($s =~ /^\s*;/ &&
2548 $function_name ne 'uninitialized_var')
2550 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2553 if ($paren_space =~ /\n/) {
2554 ERROR
("arguments for function declarations should follow identifier\n" . $herecurr);
2557 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2558 $stat =~ /^.\s*extern\s+/)
2560 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2563 # check for pointless casting of g_malloc return
2564 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2566 ERROR
("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2568 ERROR
("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2572 # check for gcc specific __FUNCTION__
2573 if ($line =~ /__FUNCTION__/) {
2574 ERROR
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2577 # recommend qemu_strto* over strto* for numeric conversions
2578 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2579 ERROR
("consider using qemu_$1 in preference to $1\n" . $herecurr);
2581 # recommend sigaction over signal for portability, when establishing a handler
2582 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2583 ERROR
("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2585 # check for module_init(), use category-specific init macros explicitly please
2586 if ($line =~ /^module_init\s*\(/) {
2587 ERROR
("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2589 # check for various ops structs, ensure they are const.
2590 my $struct_ops = qr{AIOCBInfo
|
2594 DisplayChangeListenerOps
|
2598 MemoryRegionIOMMUOps
|
2604 Spice
[A
-Z
][a
-zA
-Z0
-9]*Interface
|
2606 USBDesc
[A
-Z
][a
-zA
-Z0
-9]*|
2610 if ($line !~ /\bconst\b/ &&
2611 $line =~ /\b($struct_ops)\b.*=/) {
2612 ERROR
("initializer for struct $1 should normally be const\n" .
2616 # check for %L{u,d,i} in strings
2618 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2619 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2620 $string =~ s/%%/__/g;
2621 if ($string =~ /(?<!%)%L[udi]/) {
2622 ERROR
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2627 # QEMU specific tests
2628 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2629 ERROR
("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2632 # Qemu error function tests
2634 # Find newlines in error messages
2635 my $qemu_error_funcs = qr{error_setg
|
2638 error_setg_file_open
|
2650 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2651 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2654 # Continue checking for error messages that contains newlines. This
2655 # check handles cases where string literals are spread over multiple lines.
2657 # error_report("Error msg line #1"
2658 # "Error msg line #2\n");
2659 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2660 my $continued_str_literal = qr{\+\s*\".*\"};
2662 if ($rawline =~ /$quoted_newline_regex/) {
2663 # Backtrack to first line that does not contain only a quoted literal
2664 # and assume that it is the start of the statement.
2665 my $i = $linenr - 2;
2667 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2671 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2672 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2676 # check for non-portable libc calls that have portable alternatives in QEMU
2677 if ($line =~ /\bffs\(/) {
2678 ERROR
("use ctz32() instead of ffs()\n" . $herecurr);
2680 if ($line =~ /\bffsl\(/) {
2681 ERROR
("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2683 if ($line =~ /\bffsll\(/) {
2684 ERROR
("use ctz64() instead of ffsll()\n" . $herecurr);
2686 if ($line =~ /\bbzero\(/) {
2687 ERROR
("use memset() instead of bzero()\n" . $herecurr);
2689 my $non_exit_glib_asserts = qr{g_assert_cmpstr
|
2700 g_test_assert_expected_messages
|
2701 g_test_trap_assert_passed
|
2702 g_test_trap_assert_stdout
|
2703 g_test_trap_assert_stdout_unmatched
|
2704 g_test_trap_assert_stderr
|
2705 g_test_trap_assert_stderr_unmatched
}x
;
2706 if ($realfile !~ /^tests\// &&
2707 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2708 ERROR
("Use g_assert or g_assert_not_reached\n". $herecurr);
2712 # If we have no input at all, then there is nothing to report on
2713 # so just keep quiet.
2714 if ($#rawlines == -1) {
2718 # In mailback mode only produce a report in the negative, for
2719 # things that appear to be patches.
2720 if ($mailback && ($clean == 1 || !$is_patch)) {
2724 # This is not a patch, and we are are in 'no-patch' mode so
2726 if (!$chk_patch && !$is_patch) {
2731 ERROR
("Does not appear to be a unified-diff format patch\n");
2733 if ($is_patch && $chk_signoff && $signoff == 0) {
2734 ERROR
("Missing Signed-off-by: line(s)\n");
2737 print report_dump
();
2738 if ($summary && !($clean == 1 && $quiet == 1)) {
2739 print "$filename " if ($summary_file);
2740 print "total: $cnt_error errors, $cnt_warn warnings, " .
2741 "$cnt_lines lines checked\n";
2742 print "\n" if ($quiet == 0);
2746 # If there were whitespace errors which cleanpatch can fix
2747 # then suggest that.
2748 # if ($rpt_cleaners) {
2749 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2750 # print " scripts/cleanfile\n\n";
2754 if ($clean == 1 && $quiet == 0) {
2755 print "$vname has no obvious style problems and is ready for submission.\n"
2757 if ($clean == 0 && $quiet == 0) {
2758 print "$vname has style problems, please review. If any of these errors\n";
2759 print "are false positives report them to the maintainer, see\n";
2760 print "CHECKPATCH in MAINTAINERS.\n";
2763 return ($no_warnings ?
$clean : $cnt_error == 0);